码迷,mamicode.com
首页 > 编程语言 > 详细

Spring4(7)——对 事务 的支持

时间:2018-10-21 17:50:05      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:color   with   tor   let   accounts   删除   rop   beans   final   

1.事务

满足一下四个条件:

  • 1. 原子性;
  • 2. 一致性;
  • 3. 隔离性;
  • 4. 持久性;

场景:转账 ( 成功从A转出,但未成功转入B ,出现业务的不一致性,需要事务回滚)

2.编程式事务管理(用的少)

  • Spring 提供的事务模版类:org.springframework.transaction.support.TransactionTemplate
  • 事务管理器:org.springframework.jdbc.datasource.DataSourceTransactionManager

beans.xml 事务管理器配置

<!-- jdbc事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager"></property>
</bean>

<!-- service层注入事务管理器 -->
<bean id="bankService" class="com.java1234.service.impl.BankServiceImpl">
  <property name="bankDao" ref="bankDao"></property>
  <property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>

Service层

public class BankServiceImpl implements BankService{

    private BankDao bankDao;
    private TransactionTemplate transactionTemplate;
    
    public void setBankDao(BankDao bankDao) {
        this.bankDao = bankDao;
    }
    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }

    @Override
    public void transferAccounts(final int count, final int userIdA, final int userIdB) {  //final
        // TODO Auto-generated method stub
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus arg0) {  //内部类
                // TODO Auto-generated method stub
                bankDao.outMoney(count, userIdA);
                bankDao.inMoney(count, userIdB);                
            }
        });
    }
}

  缺点:会侵入到业务逻辑代码中(看内部类部分)

 

 

3.声明式事务管理(推荐使用)

使用 XML 配置声明式事务 和 使用注解配置声明式事务;

  • 1. 使用 XML 配置声明式事务;

    命名空间加入

    <!-- tx -->
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">

    配置事务通知

    <!-- jdbc事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 配置事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>  
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        </tx:attributes>  
    </tx:advice>
    
    <!-- 配置事务切面 -->
    <aop:config>
        <!-- 配置切点 -->
        <aop:pointcut id="serviceMethod" expression="execution(* com.java1234.service.*.*(..))" />
        <!-- 配置事务通知 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
    </aop:config>

     

  • 2. 使用注解配置声明式事务;

beans.xml 中不再需要(删除以下)

<!-- 配置事务通知 -->
<!-- 配置事务切面 -->

需要加入(加入以下)

<tx:annotation-driven transaction-manager="transactionManager"/>

Service 层

@Transactional  //注解
public class BankServiceImpl implements BankService{

    private BankDao bankDao;
    
    public void setBankDao(BankDao bankDao) {
        this.bankDao = bankDao;
    }
    
    @Override
    public void transferAccounts(int count, int userIdA, int userIdB) {
        // TODO Auto-generated method stub
                // TODO Auto-generated method stub
        bankDao.outMoney(count, userIdA);
        bankDao.inMoney(count, userIdB);                
    }
}

对比:service 多时,xml配置更方便一点。

 

4.事务传播行为

事务传播行为:Spring 中,当一个 service 方法调用另外一个 service 方法的时候,因为每个 service 方法都有事务,这时候就出现了事务的嵌套;由此,就产生了事务传播行为; 在 Spring 中,通过配置 Propagation(传播),来定义事务传播行为;

  • PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
  • PROPAGATION_SUPPORTS支持当前事务,如果当前没有事务,就以非事务方式执行。
  • PROPAGATION_MANDATORY支持当前事务,如果当前没有事务,就抛出异常。
  • PROPAGATION_REQUIRES_NEW新建事务,如果当前存在事务,把当前事务挂起。
  • PROPAGATION_NOT_SUPPORTED以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
  • PROPAGATION_NEVER以非事务方式执行,如果当前存在事务,则抛出异常。

Spring4(7)——对 事务 的支持

标签:color   with   tor   let   accounts   删除   rop   beans   final   

原文地址:https://www.cnblogs.com/Cocoomg/p/9825662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!