Spring --事务回滚

有这种业务需求:java

一次业务分三个步骤完成spring

步骤1:insert table1
成功后,执行update table2
成功后,执行update table3

执行逻辑确定以下数据库

int success1 = insert table1;
if(success1 == 1)
{
    int success2 =  update table2;
    if(success2 == 1)
    {
        int success3 = update table3;
        if(success3 == 1)
        {
            //成功
        }else
            {
              //失败3
            }
    }else
        {
            //失败2
        }
}else
{
        //失败1
}

如今,存在这种状况,假设步骤1和步骤2都成功了,express

执行步骤3的时候,返回0,即success3 == 0,可是又没有报异常,这个时候事物就不会回滚,即步骤1插入成功,步骤2也更新成功了,步骤3更新失败,这时候返回给前台的信息是此次操做失败,可是步骤1和步骤2仍然影响了数据库。app

解决办法以下ui

方法0:自动回滚,controller层捕获异常,且处理异常,加tryprototype

事物配置在service层,所以service层中的方法不作异常捕获,而是放在上层(即controller层)捕获、处理。代理

//controller层
try{
    //事物层方法
}catch{
    //异常处理
}

方法1:自动回滚,加try catchcode

事物配置在service层,service层的方法捕获异常,而后跑出异常,让aop去执行回滚
事务

//service层实现方法
try{
    insert table1;
    update table2;
    update table3;
}catch(Exception e)
{
    //抛出异常
    throw new RuntimeException(); 
}

方法2:手动回滚,我目前就采用这种方法

事物配置在service层,service层的方法捕获异常,处理异常(即执行该事物回滚操做)。

//service层实现方法
import org.springframework.transaction.interceptor.TransactionAspectSupport;
//手动回滚
int success1 = insert table1;
if(success1 == 1)
{
    int success2 =  update table2;
    if(success2 == 1)
    {
        int success3 = update table3;
        if(success3 == 1)
        {
            //成功
        }else
            {
              //失败3
              //手动回滚
              TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
    }else
        {
            //失败2
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
}else
{
        //失败1
        //手动回滚
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}


spring的事物配置以下

    <!-- ========================================分隔线========================================= -->
    <!-- 配置Spring的事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 拦截器方式配置事物 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="append*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="delAndRepair" propagation="REQUIRED" />
            <tx:method name="regist*" propagation="REQUIRED" />

            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />

            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    
    <aop:config>
        <!-- 匹配com.readygo.zbhealth.service.impl这个类下面的全部方法-->
        <aop:pointcut id="transactionPointcut" expression="execution(* com.readygo.zbhealth.service.impl.*Impl.*(..))" />
        <!--把事务控制在Service层-->
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
    </aop:config>


    <!-- 配置druid监控spring jdbc -->
    <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
    </bean>
    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
        <property name="patterns">
            <list>
                <value>com.readygo.zbhealth.service.impl.*</value>
            </list>
        </property>
    </bean>
    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
    </aop:config>


spring aop 异常捕获原理

被拦截的方法须要显式的抛出异常,且该异常没有被处理,这样aop代理才能捕获到方法的异常,才能进行回滚;默认状况下,aop只能捕获runtimeexception()异常。

方法0

在controll层捕获service层方法异常,controller层处理异常。

方法1

service捕获service层方法异常,service层抛出异常,自动回滚。

方法2

service层捕获service层方法异常,service层方法本身处理异常,手动回滚。这种方式上层不须要处理异常。

相关文章
相关标签/搜索