Spring
事务不回滚的问题关键是:不能对该异常用try...catch处理,否则不会回滚事务!
@Service("systemConfigService")
public class SystemConfigServiceImpl extends AbstractGenericService<SystemConfig> implements SystemConfigService {
@Resource(name="systemConfigDAO")
private SystemConfigDAO systemConfigDAO;
@Override
public void deleteByIds(String id) {
if(id != null){
String[] ids = id.split(",");
try {
SystemConfig systemConfig = systemConfigDAO.get(2);
systemConfig.setValue("测试值");
systemConfigDAO.update(systemConfig);
// 执行如下操作,数据库将报唯一索引重复问题
// Caused by: java.sql.BatchUpdateException: Duplicate entry 'PORTAL.CITYCODE' for key 'name'
SystemConfig systemConfig2 = systemConfigDAO.get(2);
systemConfig2.setName("PORTAL.CITYCODE");
systemConfigDAO.update(systemConfig2);
}
catch (Exception e) {
log.error(e);
// 如果注释掉throw new RuntimeException,那么事务将不能回滚,因为spring捕捉不到Exception
// 解决办法,去掉try catch,或者在cache中抛出异常以便spring捕获
throw new RuntimeException("运行时出错!");
}
}
}
}
<aop:config proxy-target-class="true" /> <!--cglib事物-->
<tx:annotation-driven proxy-target-class="true"
transaction-manager="transactionManager" />
@Transactional 必须使用tx:annotation-driven 开启注解事务支持
Connection conn ;
try{
conn = DriverManager.getConnection();//①获取数据连接
conn.setAutoCommit(false); //②关闭自动提交的机制
conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); //③设置事务隔离级别
Statement stmt = conn.createStatement();
int rows = stmt.executeUpdate( "INSERT INTO t_topic VALUES(1,’tom’) " );
rows = stmt.executeUpdate( "UPDATE t_user set topic_nums = topic_nums +1 "+
"WHERE user_id = 1");
conn.commit();//④提交事务
}catch(Exception e){
…
conn.rollback();//⑤回滚事务
}finally{
…
}
Statement stmt = conn.createStatement();
int rows = stmt.executeUpdate( "INSERT INTO t_topic VALUES(1,’tom’)");
Savepoint svpt = conn.setSavepoint("savePoint1");//①设置一个保存点
rows = stmt.executeUpdate( "UPDATE t_user set topic_nums = topic_nums +1 "+
"WHERE user_id = 1");
…
//②回滚到①处的savePoint1,①之前的SQL操作,在整个事务提交后依然提交,
//但①到②之间的SQL操作被撤销了
conn.rollback(svpt);
…
conn.commit();//③提交事务
分享到:
相关推荐
spring事务与数据库操作
Spring事务管理Demo
Spring事务流程图时序图Spring事务流程图时序图Spring事务流程图时序图Spring事务流程图时序图
Spring事务操作示例(四种方式),包含完整代码和数据库文件(基于MySQL,在项目sql文件夹中),可运行,学习Spring事务详见博客:http://blog.csdn.net/daijin888888/article/details/51822257
Spring事务原理、Spring事务配置的五种方式
spring事务配置详解 spring事务配置详解
spring 事务spring 事务spring 事务spring 事务spring 事务
spring 事务传播 demo
Spring事务管理教程,详细讲解了Spring中的事务管理,包括声明式事务,注解式事务,以及事务配置等等
spring事务源码解析
spring 事务传播与隔离级别DEMO 脏读、重复读、幻影读 spring 事务传播与隔离级别DEMO 脏读、重复读、幻影读
Spring事务管理.pdf 1.资料 2.本地事务与分布式事务 3.编程式模型 4.宣告式模型
此ppt中前半部分通过spring事务的60道题的测试,摸底对事务的掌握情况,后半部分,对spring中的事务属性(传播行为、隔离级别、回滚规则、事务超时、是否只读)进行说明
本文档介绍了spring事务隔离级别的使用及详解
包含spring事务管理案例的项目源码和说明文档
Synchronized锁在Spring事务管理下,导致线程不安全。
spring事务管理几种方式代码实例:涉及编程式事务,声明式事务之拦截器代理方式、AOP切面通知方式、AspectJ注解方式,通过不同方式实例代码展现,总结spring事务管理的一般规律,从宏观上加深理解spring事务管理特性...
spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务...
Spring事务管理中所需要的jar包是 spring-tx-3.2.0.RELEASE.jar
最详细的Spring 事务讲解 让你彻底明白