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了。但是却没有提交或者退回导致的。

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

JDBC的api文档是这样对setAutoCommit方法解释的:  

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

      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方法)  

      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.  

另外可以参考官方API关于事务这一块的说明:

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

转载于:https://my.oschina.net/u/2308739/blog/684951

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值