如果没有如果,也许永远都发现不了这个问题,除非数据出现问题没有回滚。
由于一同事测试事务问题,发现数据不对,事务没有回滚。项目采用shiro+struts2+spring+hibernate,事务配置采用声明式事务。
用于在struts2的action层 aop注解权限,所用使用了cglib动态代理,如下:
<aop:aspectj-autoproxy proxy-target-class="true"/>
cglib的动态代理是基于任意类的,而JDK的动态代理是必须要实现接口的。而我们系统中service都是基于接口实现的。 事务配置如下:
<aop:config>
<aop:pointcut id="allServiceMethod" expression="execution(* com.acts.web.*.service.*.*(..))" />
<aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
</aop:config>
表达式说明: 在service包中定义的任意方法的执行:
execution(* com.acts.web.*.service.*.*(..))
在service包或其子包中定义的任意方法的执行:
execution(* com.acts.web.*.service..*.*(..))
如果使用JDK代理,第一种实现完全没有问题,但是如果改为cglib就会导致事务无法切入。 所以改为一下:
<aop:config>
<aop:pointcut id="allServiceMethod" expression="execution(* com.acts.web.*.service..*.*(..))" />
<aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
</aop:config>