首页 > 编程语言 > 详细

Spring_23_Spring中的事务管理

时间:2021-05-12 20:23:37      阅读:11      评论:0      收藏:0      [点我收藏+]

Spring声明式事务

* 事务回顾

    - 一个事务中包含多个操作,要么所有操作都成功,要么所有操作都失败,不允许单独一个操作成功或失败。

    - 事务在项目开发中十分重要,涉及到数据的一致性问题(ACID)

    - ACID : 原子性  一致性   隔离性   持久性


* Spring中的事务分为 编程式事务 和 声明式事务

    - 声明式事务代码是通过AOP 横切进去的不影响原来的代码

    - 编程式事务需要在代码中进行事务的管理,通过try catch进行实现。 transactionManager.commit()


* 声明式事务的配置流程   结合AOP实现事务的织入

   - 需要先引入配置文件
        xmlns:tx="http://www.springframework.org/schema/tx"
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
   


   - 第一步: 配置声明式事务  

        <!--配置声明式事务-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <constructor-arg ref="dataSource" />
        </bean>



    - 第二步: 配置事务的通知

       <!--配置事务通知-->
      <tx:advice id="txAdvice"   transaction-manager="transactionManager">
          <!--要给哪些方法配置事务-->   <!--配置事务的传播特性-->
          <tx:attributes>
              <tx:method name="add"     propagation="REQUIRED"/>
              <tx:method name="delete"  propagation="REQUIRED"/>
              <tx:method name="update"  propagation="REQUIRED"/>
              <tx:method name="query"   read-only="true"/>
              <tx:method name="*"       propagation="REQUIRED"/>
          </tx:attributes>
      </tx:advice>



   - 第三步: 配置事务的切入

       <!--配置事务切入位置-->
      <aop:config>
          <aop:pointcut id="txPointCut"   expression="execution(* com.shi.mapper.*.*(..))"/>
          <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
      </aop:config>

Spring_23_Spring中的事务管理

原文:https://www.cnblogs.com/szqengr/p/14760890.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!