• spring 中的声明式事务控制


    JavaEE 体系进行分层开发,事务处理位于业务层,Spring 提供了分层设计业务层的事务处理解决方案。
      spring 框架为我们提供了一组事务控制的接口,这组接口是在spring-tx-x.x.x.RELEASE.jar 中。
      spring 的事务控制都是基于 AOP 的,它既可以使用编程的方式实现,也可以使用配置的方式实现。

    Spring 中事务控制的 API 介绍

      PlatformTransactionManager:此接口是 spring 的事务管理器,它里面提供了常用的操作事务的方法

      

       在开发中都是使用它的实现类:

        真正管理事务的对象
          org.springframework.jdbc.datasource.DataSourceTransactionManager 使用 Spring JDBC 或 iBatis 进行持久化数据时使用
          org.springframework.orm.hibernate5.HibernateTransactionManager 使用 Hibernate 进行持久化数据时使用

      TransactionDefinition:事务的定义信息对象

        

         事务的隔离级别

          

         事务的传播行为:

          REQUIRED:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)
          SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)
          MANDATORY:使用当前的事务,如果当前没有事务,就抛出异常
          REQUERS_NEW:新建事务,如果当前在事务中,把当前事务挂起。
          NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起
          NEVER:以非事务方式运行,如果当前存在事务,抛出异常
          NESTED:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行 REQUIRED 类似的操作。

        超时时间:

          默认值是-1,没有超时限制。如果有,以秒为单位进行设置。

        是否是只读事务:

          建议查询时设置为只读。

       TransactionStatus:此接口提供的是事务具体的运行状态

      

    基于 XML 的声明式事务控制(配置方式 )

      1.导入需要的坐标

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.7</version>
    </dependency>

      2.创建 spring 的配置文件并导入约束,需要导入 aop 和 tx 两个名称空间

    <?xml version="1.0" encoding="UTF-8"?>
    <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: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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    </beans>

      3.配置事务步骤

        第一步:配置事务管理器

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

        第二步:配置事务的通知引用事务管理器

    <!-- 事务的配置 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    </tx:advice>

        第三步:配置事务的属性

    <!--在 tx:advice 标签内部 配置事务的属性 -->
    <tx:attributes>
      <!-- 指定方法名称:是业务核心方法
        read-only:是否是只读事务。默认 false,不只读。
        isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。
        propagation:指定事务的传播行为。
        timeout:指定超时时间。默认值为:-1。永不超时。
        rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。
                没有默认值,任何异常都回滚。
        no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。
                没有默认值,任何异常都回滚。
      -->
      <tx:method name="*" read-only="false" propagation="REQUIRED"/>
      <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
    </tx:attributes>

        第四步:配置 AOP 切入点表达式

    <!-- 配置 aop -->
    <aop:config>
      <!-- 配置切入点表达式 -->
      <aop:pointcut expression="execution(* com.fgy.service.impl.*.*(..))" id="pt1"/>
    </aop:config>

        第五步:配置切入点表达式和事务通知的对应关系

    <!-- 在 aop:config 标签内部:建立事务的通知和切入点表达式的关系 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>

    基于注解的配置:

      1.导入需要的坐标

        ......

      2.创建 spring 的配置文件导入约束并配置扫描的包

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      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">
      <!-- 配置 spring 创建容器时要扫描的包 -->
      <context:component-scan base-package="com.fgy"></context:component-scan>
    </beans>

      3.配置步骤

        第一步:配置事务管理器,并注入数据源

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

        第二步:在业务层使用 @Transactional 注解

    @Service("accountService")
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public class AccountServiceImpl implements IAccountService {
      @Autowired
      private IAccountDao accountDao;
      @Override   
    public Account findAccountById(Integer id) {     return accountDao.findAccountById(id);   }
      @Override   @Transactional(readOnly
    =false,propagation=Propagation.REQUIRED)   public void transfer(String sourceName, String targeName, Float money) {     //1.根据名称查询两个账户     Account source = accountDao.findAccountByName(sourceName);     Account target = accountDao.findAccountByName(targeName);     //2.修改两个账户的金额     source.setMoney(source.getMoney()-money);//转出账户减钱     target.setMoney(target.getMoney()+money);//转入账户加钱     //3.更新两个账户     accountDao.updateAccount(source);     //int i=1/0;     accountDao.updateAccount(target);   } }

          该注解的属性和 xml 中的属性含义一致。该注解可以出现在接口上,类上和方法上。
            出现接口上,表示该接口的所有实现类都有事务支持。
            出现在类上,表示类中所有方法有事务支持
            出现在方法上,表示方法有事务支持。
            以上三个位置的优先级:方法 > 类 > 接口

        第三步:在配置文件中开启 spring 对注解事务的支持

    <!-- 开启 spring 对注解事务的支持 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

          完全不使用 xml 的配置方式

    @Configuration
    @EnableTransactionManagement
    public class SpringTxConfiguration {
      //里面配置数据源,配置 JdbcTemplate,配置事务管理器。
    }

    @TransactionalEventListener 注解使用:https://www.jianshu.com/p/88b76e870c63

  • 相关阅读:
    OneNote 2010 文字识别
    Windows 7 添加网络共享打印机
    logstash
    filebeat
    记elk打包时的问题
    elasticsearch.yml
    zabbix 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作
    mac装brew
    snmp监控
    博科光纤交换机端口别名映射脚本
  • 原文地址:https://www.cnblogs.com/roadlandscape/p/12307361.html
Copyright © 2020-2023  润新知