ssm的事物回滚简解

一场景:希望一个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个对数据库操作的类。并且有循环对数据库插入的操作。

循环插入报错。本身循化插入的数据回滚,对其他表的操作也进行了回滚。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值