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的更多相关文章
- EXECUTE 后的事务计数指示缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。上一计数 = 1,当前计数 = 2
理解这一句话: 一个begin tran会增加一个事务计数器,要有相同数量的commit与之对应,而rollback可以回滚全部计数器 这个错误一般是出现在嵌套事务中. 测试环境 sql 2008 例 ...
- jdbc 事物 commit 和rollback方法
package transaction; import jdbc.utils.*; import java.sql.Connection; import java.sql.PreparedStatem ...
- Fescar: Fast & Easy Commit And Rollback
Fescar is an easy-to-use, high-performance, java based, open source distributed transaction solution ...
- mysql事务,START TRANSACTION, COMMIT和ROLLBACK,SET AUTOCOMMIT语法
http://yulei568.blog.163.com/blog/static/135886720071012444422/ MyISAM不支持 START TRANSACTION | BEGIN ...
- flume-sink报错 java.lang.IllegalStateException: close() called when transaction is OPEN - you must either commit or rollback first
1. 确认代码无误(根据情况修改,表示若获得不了数据不会自动commit或者rollback): Event event = channel.take(); if (event == null) { ...
- 14.5.2.2 autocommit, Commit, and Rollback
14.5.2.2 autocommit, Commit, and Rollback 在InnoDB,所有的用户活动发生在一个事务里, 如果自动提交模式是启用的, 每个SQL语句形成一个单独的事务.默认 ...
- 14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚
14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚 如果自动提交模式被启用,在InnoDB里, 所有的用户活动发生在一个事务里, 每个SQL语句 ...
- 说commit,rollback
事务执行失败后,看做的是commit还是rollback:commit是把执行成功的部分提交了,rollback就是全回滚了.如果rollback失败了,此时不处理,等到客户端断开,MySQL内部默认 ...
- oracle 基础知识(五)--回滚(commit和rollback)
一,commit 01,commit干了啥 commit 就是提交的意思.也就是当你把99%的东西都做好了,然后你执行最后一步的操作...再commit前的话你可能啪啪啪啪啪,敲了几百条sql DML ...
随机推荐
- SQL Server中的高可用性(2)----文件与文件组
在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...
- [高并发]Java高并发编程系列开山篇--线程实现
Java是最早开始有并发的语言之一,再过去传统多任务的模式下,人们发现很难解决一些更为复杂的问题,这个时候我们就有了并发. 引用 多线程比多任务更加有挑战.多线程是在同一个程序内部并行执行,因此会对相 ...
- 多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
前言:刚学习了一段机器学习,最近需要重构一个java项目,又赶过来看java.大多是线程代码,没办法,那时候总觉得多线程是个很难的部分很少用到,所以一直没下决定去啃,那些年留下的坑,总是得自己跳进去填 ...
- ImageView缩放选项
ImageView.ScaleType 将图片边界缩放到所在view边界时的缩放选项. Options for scaling the bounds of an image to the bounds ...
- ABP文档 - Mvc 控制器
文档目录 本节内容: 简介 AbpController基类 本地化 其它 过滤 异常处理和结果包装 审计日志 验证 授权 工作单元 反伪造 模型绑定器 简介 ABP通过nuget包Abp.Web.Mv ...
- DDD 领域驱动设计-商品建模之路
最近在做电商业务中,有关商品业务改版的一些东西,后端的架构设计采用现在很流行的微服务,有关微服务的简单概念: 微服务是一种架构风格,一个大型复杂软件应用由一个或多个微服务组成.系统中的各个微服务可被独 ...
- so 问题来了,你现在值多少钱?
年底了一大帮人都写着年底总结,总结一年做过的事.错过的事和做错的事.增长了多少本事,找没找到女朋友……来年做好升职加薪,要么做跳槽的准备,程序猿又开始浮躁了……. so 问题来了,你现在值多少钱? 这 ...
- 重新认识了下Entity Framework
什么是Entity Framework Entity Framework是一个对象关系映射O/RM框架. Entity Framework让开发者可以像操作领域对象(domain-specific o ...
- NSStringCompareOptions
typedefNS_OPTIONS(NSUInteger, NSStringCompareOptions) { NSCaseInsensitiveSearch = 1, //不区分大小写比较 N ...
- 命名sql数据集
所谓的命名sql其实也就是数据库里的sql语句,普元EOS里做了一定的封装,以方便在程序中的使用. 命名SQL的基本元素包括: 1. <parameterMap> parameterMap ...