MySql 中的setAutoCommit方法


引言


setAutoCommit方法用一句話說就是用來保持事務完整性。一個系統的更新操作可能涉及多張表,這個時候,就須要用多個Sql語句來實現,實際上我認為這個東西就是用來實現事務的。


當我們進行多條數據進行增刪改的時候,一旦在一句sql中出現了錯誤,就會出現有部分數據已經成功。而后面的數據就沒有辦法運行。這個時候,就會出現臟數據。


因此我們使用setAutoCommit方法,這種方法有一個參數。參數值為Boolean,當true的時候可啟用自己主動提交模式,false可禁用該模式。


凝視中有一句話是這樣說的:Newlycreated Connection objects are in auto-commit mode by default, which means thatindividual SQL statements are committed automatically when the statement iscompleted. To be able to group SQL statements intotransactions and commit them or roll them back as a unit, auto-commit must bedisabled by calling the method setAutoCommit with false as its argument. Whenauto-commit is disabled, the user must call either the commit or rollbackmethod explicitly to end a transaction.翻譯過來是這種:假設連接處於自己主動提交模式下。則其全部的SQL語句將作為單個事務執行並提交。否則,其SQL語句將作為事務組,直到調用Commit方法或rollback方法為止。

默認情況下,新連接處於自己主動提交模式。


簡單來說,


以下的代碼:


<span style="font-size:18px;">int[] result =null;
	    con.setAutoCommit(false);   
	    Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,   
	                                       ResultSet.CONCUR_READ_ONLY);   
	    String[] SqlString= null;
	    for(String strvalue : SqlString){   	    	
	       stmt.execute(strvalue);   
	    }   
	    con.commit(); 
	    return result;</span>


能夠看到。假設代碼沒有出錯,彈幕我們就運行Commit沒有問題,可是一旦出錯。我們應該運行數據的rollback,可是該代碼沒有進行處理。這個時候。就會出現鎖,將表鎖住。這個鎖就沒有機會釋放。


因此,我們應該這樣寫:


<span style="font-size:18px;">    	 boolean result = false;
    	 try{
		    con.setAutoCommit(false);   
		    Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,   
		                                       ResultSet.CONCUR_READ_ONLY);   
		    String[] SqlString= null;
		    for(String strvalue : SqlString){   	    	
		    	result = stmt.execute(strvalue);   
		    }   
		    con.commit(); 
    	 }catch(Throwable e){
             if(con!=null){
                 try {
                     con.rollback();
                 } catch (SQLException e1) {
                     e1.printStackTrace();
                 }
             }
    	 }finally{
             if(con!=null){
                 try {
                     con.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }
    	 }
	    return result;</span>

因此,我們一定不要小看了這個問題,這個問題一旦出現,性能就會收到非常大的影響。


假設我們將SQL語句作為單個事務進行處理的話:


<span style="font-size:18px;">  /**
     * 使用PreparedStatement加批量的方法
     * @return
     */
    public int[] executeUpdateMore(){    
    	int[] result=null;    	
    	try{   
    		PreparedStatement prest =con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
    	     for(List sqlValueString : sqlValue){    	    
    	    	 for(int i=0;i<sqlValueString.size();i++){
    	     		try {
    	     			prest.setObject(i+1,sqlValueString.get(i));
    	 			} catch (SQLException e) {
    	 				// TODO Auto-generated catch block
    	 				e.printStackTrace();
    	 			}    	 				   	 			
    	     	}
    	    	prest.addBatch();
    	     }
    	     prest.executeBatch();  
    	 /*    con.commit();*/   
    	     this.closeAll(con, prest, null);
    	} catch (SQLException ex){   
    	  Logger.getLogger(Dbhelper.class.getName()).log(Level.SEVERE, null,ex);   
    	} 
    	return result;       
    } </span>

假設我們將SQL語句作為事務組來處理的話。我們就要這樣寫:


<span style="font-size:18px;">    /**
     * 使用PreparedStatement加批量的方法,strvalue:
     * "INSERT INTOadlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3','localhost','20081009',8,'23123')"
     * @return
     * @throws SQLException 
     */
    public boolean executeUpdateMoreNotAuto() throws SQLException{   
    	
    	 boolean result = false;
    	 try{
		    con.setAutoCommit(false);   
		    Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,   
		                                       ResultSet.CONCUR_READ_ONLY);   
		    String[] SqlString= null;
		    for(String strvalue : SqlString){   	    	
		    	result = stmt.execute(strvalue);   
		    }   
		    con.commit(); 
    	 }catch(Throwable e){
             if(con!=null){
                 try {
                     con.rollback();
                 } catch (SQLException e1) {
                     e1.printStackTrace();
                 }
             }
    	 }finally{
             if(con!=null){
                 try {
                     con.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }
    	 }
	    return result;
    }</span>

結束語:


一定要記住處理完之后要提交或者回滾奧!






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM