Spring声明式事务配置详解

Spring支持编程式事务管理和声明式的事务管理。

编程式事务管理


  将事务管理代码嵌到业务方法中来控制事务的提交和回滚

  缺点:必须在每个事务操作业务逻辑中包含额外的事务管理代码

声明式事务管理
  一般情况下比编程式事务好用。

  将事务管理代码从业务方法中分离出来,以声明的方式来实现事务管理。

  将事务管理作为横切关注点,通过aop方法模块化。Spring中通过Spring AOP框架支持声明式事务管理。

下面是声明式事务的两种实现方法

1、AOP通知管理事务

根据方法名自动为对应的方法添加事务

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

<!-- 2. 配置事务属性 -->
<!--<tx:advice>元素声明事务通知-->
<tx:advice id="txAdvice" 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="get*" propagation="REQUIRED" />
            <tx:method name="datagrid*" propagation="REQUIRED" />

            <tx:method name="query*" propagation="SUPPORTS"
                read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS"
                read-only="true" />
            <tx:method name="find*" propagation="SUPPORTS"
                read-only="true" />
            <tx:method name="load*" propagation="SUPPORTS"
                read-only="true" />
            <tx:method name="search*" propagation="SUPPORTS"
                read-only="true" />
            <tx:method name="count*" propagation="SUPPORTS"
                read-only="true" />
            <tx:method name="truncate*" propagation="SUPPORTS"
                read-only="true" />
            <tx:method name="menu*" propagation="SUPPORTS"
                read-only="true" />
            <tx:method name="exist*" propagation="SUPPORTS"
                read-only="true" />

            <tx:method name="exist*" propagation="SUPPORTS"
                read-only="true" />
        </tx:attributes>
</tx:advice>

<!-- 3. 配置事务切入点, 以及把事务切入点和事务属性关联起来 -->
<aop:config>
    <aop:pointcut expression="execution(* com.atguigu.spring.tx.xml.service.*.*(..))" 
        id="txPointCut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>    
</aop:config>

 

2、@Transactional 注解声明式地管理事务

Spring中注解的方式@Transactional标注事务方法。为了将方法定义为支持事务处理,可以在方法上添加@Transactional注解。根据Spring AOP基于代理机制,只能标注公有方法。如果在类上标注@Transactional注解,那么这个类中所有公有方法都会被定义为支持事务。

<!-- 配置事务管理器 -->
<bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

在方法上添加

    //添加事务注解
    //1.使用 propagation 指定事务的传播行为, 即当前的事务方法被另外一个事务方法调用时
    //如何使用事务, 默认取值为 REQUIRED, 即使用调用方法的事务
    //REQUIRES_NEW: 事务自己的事务, 调用的事务方法的事务被挂起. 
    //2.使用 isolation 指定事务的隔离级别, 最常用的取值为 READ_COMMITTED
    //3.默认情况下 Spring 的声明式事务对所有的运行时异常进行回滚. 也可以通过对应的
    //属性进行设置. 通常情况下去默认值即可. 
    //4.使用 readOnly 指定事务是否为只读. 表示这个事务只读取数据但不更新数据, 
    //这样可以帮助数据库引擎优化事务. 若真的事一个只读取数据库值的方法, 应设置 readOnly=true
    //5.使用 timeout 指定强制回滚之前事务可以占用的时间.
    @Transactional(propagation=Propagation.REQUIRES_NEW,
            isolation=Isolation.READ_COMMITTED,
            noRollbackFor={UserAccountException.class},
            rollbackFor = IOException.class,
            readOnly=false,
            timeout=3)
    @Override
    public void purchase(String username, String isbn) {}
原文地址:https://www.cnblogs.com/Donnnnnn/p/10237873.html