Spring声明式事务配置的两种方式

【正常事务操作】

    Connection connection = null;
    try {
      connection = dataSource.getConnection();
      System.out.println("连接:"+connection);
      // 开启事务(关闭自动提交 -- 防止将原子业务操作拆开提交了)
      connection.setAutoCommit(false);
      // 核心操作
      userService.insert(record);
      // 提交事务
      connection.commit();
    } catch (SQLException e) {
      // 回滚事务
      connection.rollback();
      e.printStackTrace();
    } finally {
      // 释放连接
      connection.close();
    }

【基于注解的声明式事务管理】

1. @Transactional(注解的属性指定:传播行为、隔离级别、回滚、只读、异常等值)
2. <tx:annotation-driven transaction-manager="transactionManager"/>
3. <bean id="transactionManager" class="..."> + dataSource

【基于xml配置+AOP切入点表达式的声明式事务管理】

1.配置要扫描的包,将@Service标注的类注入到IOC容器中
  <context:component-scan base-package="com.crowdfund.service" />

2.配置事务管理器:transaction manager

  # 本地事务使用 PlatformTransactionManager
  # 分布式事务的话建议使用 JtaTransactionManager作为全局事务管理器

  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"></property>
  </bean>
3.配置aop切面

    <aop:config>
        <!-- 切入点表达式: expression="execution(* *..*Service.*(..))" -->
        <aop:pointcut id="txPoint" expression="execution(* *..*Service.*(..))"  />
        <!-- 将切入点表达式与事务通知关联起来,配置事务增强 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" />
    </aop:config>

4.配置事务通知

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- 配置事务属性 -->
        <tx:attributes>
            <!-- 查询类方法:配置只读属性,这样可以让数据库对查询行为进行一定的优化 -->
            <tx:method name="get*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="query*" read-only="true" />
            <tx:method name="select*" read-only="true" />
            <!-- 增删改方法:配置事务传播行为、回滚异常-->
            <tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRES_NEW" />
            <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRES_NEW" />
            <tx:method name="create*" isolation="DEFAULT" propagation="REQUIRES_NEW" />
            <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <!-- 下面的配置则表示所有其他方法使用框架的默认事务配置 -->
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

【事务传播行为propagation说明】

# REQUIRED:默认值,表示当前方法必须工作在事务中,如果当前线程上没有已经开启的事务,则自己开始事务
         而如果当前线程有事务,则使用这个事务。但是该事务是别人的,所以有被回滚的风险。

# REQUIRES_NEW:建议使用的值,表示不管当前线程上有没有事务,都要自己开一个事务,在自己的事务里运行,
         好处是:不会受到其他事务回滚的影响。同时当前事务的异常回滚不会影响到外部事务的正常执行。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值