事物配置:
<!-- 拦截器方式配置事物 ,不在列表的方法名称spring不会管理事物-->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="select*" read-only="true" />
<tx:method name="query*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- Spring aop事务管理 -->
<aop:config>
<aop:pointcut id="transactionPointcut"
expression="execution(* s.s.m.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config>
如果Service里的方法是在配置的上半部分,如insertGoods,则Spring会管理事物,抛出异常回滚,没异常提交;
如果Service里的方法是在配置的下半部分,如getGoods,则 Spring会管理事物,不过此时只允许读数据里的数据,不允许修改数据库里的数据且抛出如下异常:
### SQL: insert into t_bs_goods_back(goods_name) values(?)### Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed; SQL []; Connection is read-only. Queries leading to data modification are not allowed; nested exception is java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
说明:此种情况不允许配置propagation。
如果Service里的方法是不在配置里,则Spring不会管理事物,无论是否抛出异常都会提交事物。