一场景:希望一个cotroller方法中调用的所有方法一个抛出问题全部数据库的操作回滚
项目环境:ssm项目
解决方法:
spring的事物管理可以使controller调用的service的实现类使用的所有dao层数据库操作出错发生回滚。
而controller调用多个service方法sercive方法之间并不会发生关联回滚的操作。
不配置spring事物回滚的方法,回滚只发生在一个dao层方法一次执行数据库错误时,
配置spring事物回滚的,可实现一个service实现类中的多个方法请求数据中一个错误全部回滚。
spring配置事物的spring-mybatis-transaction.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
default-lazy-init="true">
<bean id="caseTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="caseDataSource" />
</bean>
<tx:advice id="caseAdvice" transaction-manager="caseTransactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<!-- <tx:method name="create*" propagation="REQUIRED" rollback-for="Exception"/> -->
<tx:method name="set*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="pass*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="deny*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="Copy*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="copy*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="change*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="certifi*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="move*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="upload*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="bankSave*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="cuishouSave*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="import*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:advisor pointcut="(execution(* org.uz.dxt.service.bankbase..*.*(..)))" advice-ref="caseAdvice" />
</aop:config>
<!-- 使用AspectJ方式配置AOP -->
<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>
配置的controller.(service)方法名import开头的方法都可以发生回滚。
测试结果
一个controller方法中调用一个service方法,service实现类中有5个对数据库操作的类。并且有循环对数据库插入的操作。
循环插入报错。本身循化插入的数据回滚,对其他表的操作也进行了回滚。