你的位置:首页 > 软件开发 > Java > JDBC Tutorials: Commit or Rollback transaction in finally block

JDBC Tutorials: Commit or Rollback transaction in finally block

发布时间:2016-09-23 03:00:04
http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Commit or Rollback transaction in finally ...

http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html

 

JDBC Tutorials: Commit or Rollback transaction in finally block

 

In most of JDBC books, the transaction management idiom that is followed is, after executing the update statements commit, and if an SQLException is thrown, rollback. // Some operations Statement stmt2 = ... con.commit();The similar structure is followed in the JDBC(TM) APIThere is a severe problem with this way of commiting and rollback. The problem is we are handling only the SQLException. What will happen if a RuntimeException occured after executing the first update statement but beforethe second update statement?The transaction is opened, but neither commited nor rolled back. This will leave the data integrity into trouble. If we are reusing the same connection (as in most cases), and we commit the transaction in the next statements, we are into serious trouble. We have inconsitent data.What is the solution?Doesn't this look awkward,Whenever we use transactions we should catch a Throwable class or atleast Exception class?Use finally block // Some operations Statement stmt2 = ... success = true;}catch(SQLException e){

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:JDBC Tutorials: Commit or Rollback transaction in finally block

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录