一、概述
事务(transaction):指数据库中执行的一系列操作被视为一个逻辑单元,要么全部成功地执行,要么全部失败回滚,保证数据的一致性和完整性。
@transactional注解是spring框架提供的用于声明事务的注解,作用于类和方法上。
1.1 @transactional注解
属性 | 可选值 | 作用 |
---|---|---|
propagation | requiredrequires_newnestednot_supportedsupportsmandatory | 指定事务的传播行为,在事务嵌套时起作用,默认值为propagation.required |
isolation | default(和数据表一致)read_uncommitted(读-未提交)read_committed(读已提交)repeatable_read(可重复读)serializable(串行化) | 指定事务的隔离级别,和数据库的事务一致,默认值为isolation.default |
readonly | truefalse | 指定事务是否为只读事务,默认值为false。如果将其设置为true,表示事务只涉及读取操作 |
timeout | 数字(秒) | 指定事务的超时时间(秒),默认值为transactiondefinition.timeout_default。如果事务在指定的时间内未完成,将被自动回滚 |
rollbackfor | 异常类.class | 指定触发事务回滚的异常类型数组,默认为空。当方法抛出指定类型的异常时,事务将回滚 |
norollbackfor | 异常类.class | 指定不触发事务回滚的异常类型数组,默认为空。当方法抛出指定类型的异常时,事务将不回滚 |
rollbackforclassname | 异常类名 | 与rollbackfor类似,但是使用异常类型的完全限定名字符串来指定触发事务回滚的异常 |
norollbackforclassname | 异常类名 | 与norollbackfor类似,但是使用异常类型的完全限定名字符串来指定不触发事务回滚的异常 |
1.2 spring事务原理
spring的事务是依靠aop实现的。
是在程序运行时给代理对象创建代理类。
如果方法或类上添加了@transcation注解,spring会自动给方法或类创建一个代理类,代理类中包含了开关事务的代码和原始操作。
示意图如下(原始类指加了@transcation的类):
如果嵌套调用呢?
method1方法上有事务注解,method2没有,method1调用了method2。
会生成如下的代理类:
二、@transactional使用
2.1 事务失效的7种情况
这里用一个demo举例子:更新一条数据,我们先删除数据,再插入新数据(主键自动递增)。
我们希望删除或插入哪一方失败,数据库都能回滚。spring事务失效是指发生异常依然不回滚。
1. 同一个类中方法调用
开发中避免不了会对同⼀个类⾥⾯的⽅法调⽤,⽐如有⼀个类 test,它的⼀个⽅法 a,a 再调⽤本类的⽅法 b(不论⽅法 b 是⽤ public 还是 private 修饰),但⽅法 a 没有声明注解事务,⽽ b ⽅法有。
外部调⽤⽅法 a 之后,⽅法 b 的事务是不会起作⽤的,这也是经常犯错误的⼀个地⽅。
public class test{ //外层 public void a(category category) { this.categorydao.delete(category); this.b(category);//无事务,但有异常 } //内层 @transactional public void b(category category) { this.categorydao.insert(category); int a=2/0; } }
为什么失效:
其实这还是由于使⽤ spring aop 代理造成的,没加@transactional注解的方法不会被代理类重写,也就不会有事务。
2. 异常被 catch 住,而且没有再次抛出异常
无论是外层异常还是内层异常,只要捕获以后没有抛出异常,都不会回滚。总的来说没有异常不会回滚。
//外层 @transactional public void updatebyid(category category) { try { this.categorydao.deletebyid(category.getcid()); this.categorydao.insert(category); int a=2/0; }catch (java.lang.exception e){ system.out.println("updatebyid异常"); } }
//外层 public void update(category category) { this.delete(category); this.categorydao.insert(category); } //内层 @transactional void delete(category category) { try{ this.categorydao.delete(category.getid); int a=2/0; }catch(exception e){ } }
解决办法:
捕获后再次抛出异常。无论是内层、外层,只要重新抛出异常,就可以回滚。
//外层 public void update(category category) { this.delete(category); this.categorydao.insert(category); } //内层 @transactional void delete(category category) { try{ this.categorydao.delete(category.getid); int a=2/0; }catch(exception e){ throw new runtimeexception("service层deletebyid方法异常");//可以自定义异常 } }
结论:没有异常不会回滚。
3. 抛出runtimeexception或error以外的异常
@transcation有个属性(方法),管理何种异常会引发回滚。
默认情况下,事务只在runtimeexception和error上回滚。
抛出runtimeexception和error以外类型的异常,不会回滚。
常见的非运行时异常有:sqlexception、ioexception、filenotfoundexception、reflectiveoperationexception等等。
@transactional void delete(category category) throws sqlexception{ this.categorydao.delete(category.getid); int a=2/0; throw new sqlexception("xxx"); //抛出异常也不会回滚 }
解决办法:
使用rollbackfor属 添加 要捕获的异常类型,这样除了runtimeexception和error类型的异常,遇到exception以及它的子类的异常,也会发生回滚。
@transactional(rollbackfor = exception.class) void delete(category category) throws sqlexception{ this.categorydao.delete(category.getid); int a=2/0; throw new sqlexception("xxx"); //这下就回滚了 }
结论:使用rollbackfor属性添加要捕获的异常类型
4. 子线程内异常
删除操作新开一个线程执行,并且执行中发生异常。结果是删除回滚,插入没回滚。
多线程环境下,内外层是两个事务,事务具有隔离性,事务之间不会互相干扰。
//外层 @transactional(rollbackfor = java.lang.exception.class) public void update(category category) { thread thread=new thread(new runnable() { @override public void run() { delete(category);//删除 } }); this.categorydao.insert(category);//插入 } //内层 @transactional(rollbackfor = java.lang.exception.class) void delete(category category) { this.categorydao.delete(category.getid); int a=2/0; //异常 }
解决办法:
使用thread.uncaughtexceptionhandler接口捕获线程异常,主线程发现了异常,也跟着回滚。
注:事务还是多个事务
1.创建一个实现了thread.uncaughtexceptionhandler接口的异常处理器类,该类将负责捕获未被捕获的(没加try-catch的)线程异常并进行处理:
public class customuncaughtexceptionhandler implements thread.uncaughtexceptionhandler { @override public void uncaughtexception(thread t, throwable e) { // 在此可以处理未被捕获的线程异常 system.out.println("线程 " + t.getname() + " 发生了异常: " + e.getmessage()); } }
2.在主线程或创建的子线程中,设置自定义的异常处理器:
@transactional(rollbackfor = java.lang.exception.class) public void updatebyid(category category){ thread.setdefaultuncaughtexceptionhandler(new customuncaughtexceptionhandler());//加上这句 thread thread=new thread(new runnable() { @override public void run() { deletebyid(category.getcid());//删除 } }); thread.start(); this.categorydao.insert(category);//插入 }
结论:子线程异常抛给主线程,两者一起回滚。
5. 事务方法是private、static、final的
事务是依赖aop实现的,如果方法不能被重写,就不能生产代理类。
结论:java实现的动态代理的原理是代理类实现被代理类的相同接口、或成为被代理类的子类,然后重写相同方法
6. 数据库不支持事务
mysql的myisam存储引擎是不支持事务的,它是旧版 mysql(mysql 5.5 之前)中的默认存储引擎。
7. 设置了某些事务传播行为
在事务嵌套的时候,设置了以下传播行为,会让事务挂起(相当于没有事务)或抛异常。
- transactiondefinition.propagation_supports:如果当前存在事务,则加⼊该事务;如果当前没有事务,则以⾮事务的⽅式继续运⾏。
- transactiondefinition.propagation_not_supported:以⾮事务⽅式运⾏,如果当前存在事务,则把当前事务挂起。
- transactiondefinition.propagation_never:以⾮事务⽅式运⾏,如果当前存在事务,则抛出异常
//外层 @transactional public void a(category category) { this.categorydao.insert(category); this.b(category.getcid()); } //内层 @transactional(propagation = propagation.not_supported) public boolean b(integer cid) { int i = this.categorydao.deletebyid(cid); if(i>0) { throw new runtimeexception("xxx"); } return true; }
2.2 事务6种传播机制
默认是requierd,保证只有一个事务。
总结
spring相当多的功能都用到了动态代理,还需要对这方面知识做个总结,学无止境啊。
发表评论