• 为何添加事务管理器呢?spring事务又有哪些传播特性?


    目录

    为什么添加事务管理器

    Spring事务的传播特性

    为了测试传播特性改造项目

    声明式事务

    事务优先级的解决方式


    为什么添加事务管理器

    事务是对一系列的数据库操作(比如插入多条数据)进行统一的提交或回滚操作,如果插入成功,那么一起成功,如果中间有一条出现异常,那么回滚之前的所有操作,这样可以防止出现脏数据,防止数据库数据出现问题。
      JDBC:  Connection   con.commit();   con.rollback();
      MyBatis:  SqlSession   sqlSession.commit();  sqlSession.rollback();
      Hibernate:  Session    session.commit();   session.rollback();

      事务管理器用来生成相应技术的连接+执行语句的对象。
      如果使用MyBatis框架,必须使用DataSourceTransactionManager类完成处理

    1.  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    2.        
    3.         <property name="dataSource" ref="dataSource">property>
    4.      bean>

     

     

     

      项目中的所有事务,必须添加到业务逻辑层上。

    JDBC中是通过Connection对象进行事务管理,默认是自动提交事务,可以手工将自动提交关闭,通过commit方法进行提交,rollback方法进行回滚,如果不提交,则数据不会真正的插入到数据库中。

    Hibernate中是通过Transaction进行事务管理,处理方法与JDBC中类似。

    Spring中也有自己的事务管理机制,一般是使用TransactionMananger(事务管理器)进行管理,可以通过Spring的注入来完成此功能。
     

    Spring事务的传播特性

     


      多个事务之间的合并,互斥等都可以通过设置事务的传播特性来解决。
      常用

      PROPAGATION_REQUIRED:必被包含事务(增删改必用)
      PROPAGATION_REQUIRES_NEW:自己新开事务,不管之前是否有事务
      PROPAGATION_SUPPORTS:支持事务,如果加入的方法有事务,则支持事务,如果没有,不单开事务
      PROPAGATION_NEVER:不能运行中事务中,如果包在事务中,抛异常
      PROPAGATION_NOT_SUPPORTED:不支持事务,运行在非事务的环境
      不常用
      PROPAGATION_MANDATORY:必须包在事务中,没有事务则抛异常
      PROPAGATION_NESTED:嵌套事务

    为了测试传播特性改造项目


       

     

    1. @Service  //交给Spring去创建对象
    2.     @Transactional(propagation = Propagation.REQUIRED)
    3.     public class UsersServiceImpl implements UsersService {    
    4.         @Autowired
    5.         UsersMapper usersMapper;
    6.         @Autowired
    7.         AccountsService accountsService;
    8.         @Override
    9.         public int insert(Users users) {
    10.             int num = usersMapper.insert(users);
    11.             System.out.println("用户增加成功!num="+num);
    12.             //调用帐户的增加操作,调用的帐户的业务逻辑层的功能
    13.             num = accountsService.save(new Accounts(300,"王五","帐户好的呢!"));
    14.             return num;
    15.         }
    16.     }

    声明式事务


      Spring非常有名的事务处理方式——声明式事务。
      要求项目中的方法命名有规范
      1)完成增加操作包含    add  save  insert  set
      2)更新操作包含   update   change  modify  
      3)删除操作包含   delete   drop    remove  clear
      4)查询操作包含   select   find    search  get 

     

      
      配置事务切面时可以使用通配符 * 来匹配所有方法

    1.  
    2.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    3.         <property name="dataSource" ref="dataSource">property>
    4.     bean>
    5.    
    6.     <tx:advice id="myadvice" transaction-manager="transactionManager">
    7.         <tx:attributes>
    8.             <tx:method name="*select*" read-only="true"/>
    9.             <tx:method name="*find*" read-only="true"/>
    10.             <tx:method name="*search*" read-only="true"/>
    11.             <tx:method name="*get*" read-only="true"/>
    12.             <tx:method name="*insert*" propagation="REQUIRED" no-rollback-for="ArithmeticException"/>
    13.             <tx:method name="*add*" propagation="REQUIRED"/>
    14.             <tx:method name="*save*" propagation="REQUIRED" no-rollback-for="ArithmeticException"/>
    15.             <tx:method name="*set*" propagation="REQUIRED"/>
    16.             <tx:method name="*update*" propagation="REQUIRED"/>
    17.             <tx:method name="*change*" propagation="REQUIRED"/>
    18.             <tx:method name="*modify*" propagation="REQUIRED"/>
    19.             <tx:method name="*delete*" propagation="REQUIRED"/>
    20.             <tx:method name="*remove*" propagation="REQUIRED"/>
    21.             <tx:method name="*drop*" propagation="REQUIRED"/>
    22.             <tx:method name="*clear*" propagation="REQUIRED"/>
    23.             <tx:method name="*" propagation="SUPPORTS"/>
    24.         tx:attributes>
    25.     tx:advice>
    26.    
    27.     <aop:config>
    28.         <aop:pointcut id="mycut" expression="execution(* com.bjpowernode.service.impl.*.*(..))">aop:pointcut>
    29.         <aop:advisor  advice-ref="myadvice" pointcut-ref="mycut">aop:advisor>
    30.     aop:config>

     

    事务优先级的解决方式


    1. 方法改名

    2. 注解配置事务传播级别为开启新事务, @Transactional(read-only="false", propagation = Propagation.REQUIRES_NEW)

    3. 在原有xml扫描方式中排除带有注解的方法或类

    !(@annotation(org.springframework.transaction.annotation.Transactional) or @within(org.springframework.transaction.annotation.Transactional))

    总结
    1. 事务切面配置冲突时, 如果事务传播级别相同(默认REQUIRED), 以XML为准, 不区分加载顺序(order=x)

     

  • 相关阅读:
    Java Object类方法简要解释(equals, hashCode, toString, finalize)
    Spring cloud Sentinel介绍和安装
    NoSQL 之 Redis 高可用与持久化(二)
    sleuth+zipkin持久化和gateway设置跨域
    从零开始,开发一个 Web Office 套件(5):Mouse hover over text
    需要在html中加CSS,怎么加
    【MAPBOX基础功能】22、mapbox根据指定中心点绘制指定公里数的矩形
    计算机网络笔记【面试】
    转载-C#学习笔记-基本概念(CLR、CTS、CLS...)
    【MM小贴士】副产品 工单核算
  • 原文地址:https://blog.csdn.net/weixin_48826996/article/details/126003524