java數據庫Connection事務 – JAVA編程語言程序開發技術文章

Java代碼 
 public void deleteDeadDataImprove(String tableName, long updateTime) throws Exception 
    { 
        // 根據報表源判斷是sql 
        if(connGrpForDel == null) 
        { 
            logger.error("connGrpForDel is not initialized."); 
            throw new Exception("connGrpForDel is not initialized."); 
        } 
        Connection connection = null; 
        synchronized(this) 
        { 
            connection = this.connGrpForDel.getActiveConnection(); 
            threadLocal.set(new Long(batchNo)); 
            logger.debug(Thread.currentThread().getName() + "ok," + "threadLocal.get()=" + threadLocal.get() + ",batchNo=" + batchNo); 
        } 
        String sql = ""; 
//        Statement pstmt = null; 
        PreparedStatement pstmt = null; 
        try 
        { 
            connection.setAutoCommit(false); 
 
            // 刪除主鍵和索引 
            String dbType = DBConnectionParams.getDatabaseType(); 
            if(CommonAttrs.DATABASE_TYPE_ORACLE.equals(dbType)) 
            { 
                sql = "drop index I" + tableName; 
            } 
            else if(CommonAttrs.DATABASE_TYPE_SQLSERVER.equals(dbType)) 
            { 
                sql = "drop index " + tableName + ".I" + tableName; 
            } 
            logger.info("sql=" + sql); 
            if(sql != null && sql.length() > 0) 
            { 
//                pstmt = connection.createStatement(); 
//                pstmt.executeUpdate(sql); 
                pstmt = connection.prepareStatement(sql); 
                pstmt.executeUpdate(); 
            } 
            // 批量刪除數據 
            // boolean status = true; 
            sql = "delete from " + tableName + " where timestamp <= " + updateTime;// + " and rownum <= " + countOneBatchDel; 
            logger.debug("sql=" + sql); 
            // while(status) 
            // { 
//            int deleteCount = pstmt.executeUpdate(sql); 
            pstmt = connection.prepareStatement(sql); 
            int deleteCount = pstmt.executeUpdate(); 
            logger.info("Remove " + deleteCount +" expired records from " + tableName + " table"); 
            // 分批刪,但一次性提交 
            // if(deleteCount <= 0) 
            // { 
            // status = false; 
            // } 
            // } 
            // 創建主鍵和索引 
            sql = "create index I" + tableName + " on " + tableName + "(timestamp ASC, entityId)"; 
            logger.info("sql=" + sql); 
//            pstmt.executeUpdate(sql); 
            pstmt = connection.prepareStatement(sql); 
            pstmt.executeUpdate(); 
            connection.commit(); 
        } 
        catch(SQLException e) 
        { 
            logger.error("SQLException when deleteDeadDataImprove.", e); 
            connection.rollback(); 
        } 
        finally 
        { 
 
            if(pstmt != null) 
            { 
                pstmt.close(); 
                pstmt = null; 
            } 
            if(!connection.isClosed()) 
            { 
                // 存儲結束後,將數據存儲自動提交置為true,需要手動提交時再修改為false 
                connection.setAutoCommit(true); 
                // 釋放connection 
                this.connGrpForDel.realseConnection(connection); 
            } 
            else 
            { 
                // 如果是connection被關閉瞭,重新初始化數據池 
                reInitialize(); 
            } 
 
        } 
 
    } 

 

本文出自“gmleegmlee”
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *