最近在开发中遇到一些问题,在配置xml事务后,又在具体service方法中配置注解,结果注解并不执行。
按我原来的理解是配置xml的目的是为了具体代码的简洁,和大体的统一。而具体的方法如果有特殊的要求,应该执行配置注解。
但是我的理解并不太对。于是我做了一系列的测试。
第一次 首先我证明@Transactional注解到底有没有执行,于是我测试了readOnly参数,外面的xml配置ture,里面注解配置false。结果报错了。说明了注解的方式执行了。注意这里我xml和注解配置的是同一种事务。如果是不通类型事务那么不会报错。
结果看xml的优先级更高。但是网上有很多文章证明是注解优先级高。于是我继续做实验
第二次 我在xml配置是设置没有事务,而注解配置了回滚事务,结果方法在报错后回滚了。
结果看执行了注解,这注解优先级又比xml高了。这里我想无论是xml方式还是注解的方式,其实都是AOP,那么两种方式都执行了,而不是执行其中一个。执行了xml开始一个没有事务,又执行了一个注解有事务,所以还是有事务。
第三次 我在xml配置是设置回滚事务,而在注解里面配置了never事务(never:以非事务方式执行操作,如果当前事务存在则抛出异常。)
结果是执行方法时,报错回滚了,说明先执行了注解事务,后执行xml事务。那为什么先加载注解事务呢?难道spring加载的顺序?
我现在配置是
<!-- 使用annotation注解方式配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 使用JDBC事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="masterdataSource" />
</bean>
<!-- AOP配置事务 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="NESTED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="modify*" propagation="NESTED" rollback-for="Exception"/>
<tx:method name="edit*" propagation="NESTED" rollback-for="Exception"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="save*" propagation="NESTED" rollback-for="Exception"/>
<tx:method name="send*" propagation="NESTED" rollback-for="Exception"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="count*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置AOP切面 -->
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.demo..service.*.*(..))"/>
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice"/>
</aop:config>
第四次 我换了顺序,把tx:annotation-driven放在了xml配置后面
结果 never报错了。原因就是这回先加载xml配置设置了一个回滚事务。又加载注解的never事务报错了,因为已经存在事务了。难道就看谁写前面?一定有默认参数吧!是的,有
<!-- 使用annotation注解方式配置事务 order为优先级 -->
<tx:annotation-driven transaction-manager="transactionManager" order="1"/>
<!-- AOP配置事务 order为优先级-->
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* com.wkb..service.*.*(..))"/>
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" order="0" />
</aop:config>
总结:
xml配置与注解配置都执行,默认顺序谁先加载谁先执行,指定执行顺序 添加order参数
建议:
应该先执行xml 后 执行注解
xml 配置的是统一的。但是可能我想在具体的方法里搞特殊,就自己加注解
例如xml配置了REQUIRED回滚 而我的具体的方法不想回滚但是还要用insert开头,就可以在注解配置
@Transactional(propagation=Propagation.PROPAGATION_NOT_SUPPORTED, noRollbackFor=Exception.class)
这样所有的insert开头的都有回滚事务,就这个加注解的没有。