`

spring aop的事务

 
阅读更多
by 2016-09-03
http://www.cnblogs.com/leiOOlei/p/3557643.html


=========
因为session是用SqlSessionTemplate来注入的,所以无法自动管理session的closed,即当多个sql操作的时候无法保证过程中某一个sql操作  以前所有的操作都不能commit,所以要采用事务的管理,开始一直不行,各种方法,最后才发现是因为是我在aop配置事务执行的方法里没有throw new RuntimeException();
我是直接
try{

catch{}
return Map
在这里sql操作都是在try中操作,如果有异常,则抛出来的异常被捕获了,不能抛到方法外,就不能被aop的事务拦截到,也就不能执行回滚操作,

xml配置
<aop:config>
<aop:pointcut expression="execution(* xx.addGroup(..))" id="modelpoint"/>
<aop:advisor advice-ref="userM"  pointcut-ref="modelpoint"/>
</aop:config>

<tx:advice id="userM" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="addGroup"  propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
=========
在<tx:method 中的也可以指定对特定的异常才回滚用
rollback-for="java.lang.Exception",不写就是对所有Unchecked Exceptions拦截



spring 描述是遇到Unchecked Exceptions才回滚,所以我们要对添加事务的方法千万不能捕获异常,即使捕获了也要在catch中抛出来,不然方法不能抛出异常,aop也就接受不到Unchecked Exceptions,就自然回滚失效了。


Exceptions类:
http://nino789pzw.blog.163.com/blog/static/16453229420124615210781/


事务级别:http://blog.csdn.net/fg2006/article/details/6937413
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics