Java Connection.setAutoCommit使用的注意事項

1  Connection con = null;  
2      try{  
3          con = getConnection();  
4          con.setAutoCommit(false);  
           /* 
5          * update USER set name=’winson’ where id=’000001’; 
            */  
6          con.commit();  
7       }finally{  
8          if(con!=null){  
9              try {  
10                 con.close();  
11             } catch (SQLException e) {  
12                 e.printStackTrace();  
13             }  
           }  
       }

如上是使用commit有問題的代碼。是由於setAutoCommit(false)爲false了。可是卻沒有提交或者退回致使的。html

附:在oracle上查看鎖的方法:select * from v$lock_object或者select * from v$lock.  java

JDBC的api文檔是這樣對setAutoCommit方法解釋的:  api

Sets the connection's auto-commit mode to enableAutoCommit.  oracle

      Newly created Connection objects are in auto-commit mode by default, which means that individual SQL statements are committed automatically when the statement is completed. To be able to group SQL statements into transactions and commit them or roll them back as a unit, auto-commit must be disabled by calling the method setAutoCommit with false as its argument. When auto-commit is disabled, the user must call either the commit or rollback method explicitly to end a transaction.(必定不能大意哦,若是設置成非自動提交,在最後必定要調用commit或者rollback方法)  this

      The commit occurs when the statement completes or the next execute occurs, whichever comes first. In the case of statements returning a ResultSet object, the statement completes when the last row of the result set has been retrieved or the ResultSet object has been closed. In advanced cases, a single statement may return multiple results as well as output parameter values. In this case, the commit may occur when all results and output parameter values have been retrieved, or the commit may occur after each result is retrieved.  code

另外能夠參考官方API關於事務這一塊的說明:htm

https://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html事務

相關文章
相關標籤/搜索