星空网 > 软件开发 > Java

JDBC Tutorials: Commit or Rollback transaction in finally block

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.
That is,


Connection con = null;
try{
con = //...
con.setAutoCommit(false);

Statement stmt1 = ...
stmt1.executeUpdate();

// Some operations

Statement stmt2 = ...
stmt2.executeUpdate();

con.commit();
con.setAutoCommit(true);
}catch(SQLException e){
if(con!=null){
try{
con.rollback();
}catch(SQLException e){
// Log the error...
}
}
}


The similar structure is followed in the JDBC(TM) API
Tutorial and Reference from the Sun Microsystems. Have a look at theTransactions Tutorial and the Sample code provided.

There 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?
Catch Exception instead of SQLException
A simpler and not recommended solution is, catch all the execeptions, including RuntimeException. Even now, what if an Error is thrown, say OutOfMemoryError or some VirtualMachineError or something else? What ever happens in the code, we should either the database should be committed or rolledback. So, the worst thing is we should catch the Throwable class, instead of Exception.

Doesn't this look awkward,Whenever we use transactions we should catch a Throwable class or atleast Exception class?

Use finally block
A clean solution and yet simple solution is, use finally block. Since it is always guaranteed that the finally block will be executed even when any Exception is thrown or even when the method is returned.



Connection con = null;
boolean success = false;
try{
con = //...
con.setAutoCommit(false);

Statement stmt1 = ...
stmt1.executeUpdate();

// Some operations

Statement stmt2 = ...
stmt2.executeUpdate();

success = true;

}catch(SQLException e){
success = false;
}finally{
if(con!=null){
try{
if(success){
con.commit();
con.setAutoCommit(true);
}else{
con.rollback();
}
}catch(SQLException e){
// Log the error...
}
}
}

 






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

关键词:

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

韩国电商平台10月份热度指数新鲜出炉,Coupang排第一,11街排第二:https://www.ikjzd.com/articles/132015
2020年跨境电商Google购物广告终极指南(上):https://www.ikjzd.com/articles/132016
亚马逊封号的种类及恢复账号:https://www.ikjzd.com/articles/132018
亚马逊卖家请注意!!小心踩雷!:https://www.ikjzd.com/articles/132020
虚假评论的存在日益增加,亚马逊又想出什么方法阻止?:https://www.ikjzd.com/articles/132021
双百挑战令-暨2020年山西跨境电商交流会:https://www.ikjzd.com/articles/132024
亚马逊以图搜图功能再更新,99%运营不知道!:https://www.kjdsnews.com/a/1842135.html
短视频广告剪辑技巧 :https://www.kjdsnews.com/a/1842136.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流