zoukankan      html  css  js  c++  java
  • Spring中的事务控制

    Spring中事务控制的API介绍

    PlatformTransactionManager

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

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

    真正管理事务的对象

    org.springframework.jdbc.datasource.DataSourceTransactionManager 使用Spring JDBC或iBatis 进行持久化数据时使用 org.springframework.orm.hibernate5.HibernateTransactionManager 使用Hibernate版本进行持久化数据时使用

     TransactionDefinition

    它是事务的定义信息对象

    public interface TransactionDefinition {
        int PROPAGATION_REQUIRED = 0;
        int PROPAGATION_SUPPORTS = 1;
        int PROPAGATION_MANDATORY = 2;
        int PROPAGATION_REQUIRES_NEW = 3;
        int PROPAGATION_NOT_SUPPORTED = 4;
        int PROPAGATION_NEVER = 5;
        int PROPAGATION_NESTED = 6;
        int ISOLATION_DEFAULT = -1;
        int ISOLATION_READ_UNCOMMITTED = 1;
        int ISOLATION_READ_COMMITTED = 2;
        int ISOLATION_REPEATABLE_READ = 4;
        int ISOLATION_SERIALIZABLE = 8;
        int TIMEOUT_DEFAULT = -1;
    
        int getPropagationBehavior();
        int getIsolationLevel();
        int getTimeout();
        boolean isReadOnly();
        @Nullable
        String getName();
    }
    getPropagationBehavior();   获取事务传播行为
    getIsolationLevel();   获取事务隔离级别
    getTimeout();    获取事务超时时间
    isReadOnly();    获取事务是否只读
    String getName();   获取事务对象名称

     事务的隔离级别

     事务及事务隔离级别

     

     事务的传播行为

    REQUIRED:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值) 

    SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)

    MANDATORY:使用当前的事务,如果当前没有事务,就抛出异常

    REQUERS_NEW:新建事务,如果当前在事务中,把当前事务挂起。

    NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起

    NEVER:以非事务方式运行,如果当前存在事务,抛出异常

    NESTED:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行REQUIRED类似的操作。

     超时时间

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

     是否是只读事务

    建议查询时设置为只读。

    TransactionStatus

     该接口描述了某个时间点上事务对象的状态信息

    public interface TransactionStatus extends SavepointManager, Flushable {
        boolean isNewTransaction();  //获取事务是否为新的事务
        boolean hasSavepoint();  //获取是否存在存储点
        void setRollbackOnly();  //设置事务是否回滚
        boolean isRollbackOnly();   //设置事务回滚
        void flush();    //刷新事务
        boolean isCompleted();  //获取事务是否完成
    }

    基于XML的声明式事务控制

     导入约束

    <?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>

    1. 配置事务管理器

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    2.配置事务的通知引用事务管理器

     <tx:advice id="txAdvice"  transaction-manager="transactionManager"></tx:advice>

    3. 配置事务的属性

    <!--在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>

    4. 配置AOP切入点表达式

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

    5. 配置切入点表达式和事务通知的对应关系

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

    示例:

        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
        <!-- 配置事务的通知-->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <!-- 配置事务的属性
                    isolation:用于指定事务的隔离级别。默认值是DEFAULT,表示使用数据库的默认隔离级别。
                    propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。
                    read-only:用于指定事务是否只读。只有查询方法才能设置为true。默认值是false,表示读写。
                    timeout:用于指定事务的超时时间,默认值是-1,表示永不超时。如果指定了数值,以秒为单位。
                    rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值。表示任何异常都回滚。
                    no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时事务回滚。没有默认值。表示任何异常都回滚。
            -->
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED" read-only="false"/>
                <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
            </tx:attributes>
        </tx:advice>
    
        <!-- 配置aop-->
        <aop:config>
            <!-- 配置切入点表达式-->
            <aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut>
            <!--建立切入点表达式和事务通知的对应关系 -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
        </aop:config>

    基于注解的配置方式

     导入约束

    <?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.itheima"></context:component-scan>
    <!-- 配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置spring提供的内置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
      <property name="url" value="jdbc:mysql://localhost:3306/spring_day02"></property>
      <property name="username" value="root"></property>
      <property name="password" value="1234"></property>
      </bean>
    </beans>

    1. 配置事务管理器并注入数据源

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </bean>

    2. 在配置文件中开启spring对注解事务的支持

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

    3. 在业务层使用@Transactional注解

    该注解的属性和xml中的属性含义一致。该注解可以出现在接口上,类上和方法上。

    出现接口上,表示该接口的所有实现类都有事务支持。

    出现在类上,表示类中所有方法有事务支持

    出现在方法上,表示方法有事务支持。

    以上三个位置的优先级:方法>类>接口

    @Service("accountService") 
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public class AccountServiceImpl implements IAccountService {.......}
  • 相关阅读:
    火狐firefox进行post提交测试
    spring cloud:config-eureka-refresh
    spring cloud:config
    使用Docker部署Gitlab
    Docker配置加速器
    spring cloud:eureka
    Sql Server 出现此数据库没有有效所有者问题
    将VS2012的项目转化成VS2010
    SQL Server 2008将数据库数据导出到脚本
    Sql Server 显示插入Identity字段
  • 原文地址:https://www.cnblogs.com/mkl7/p/10719708.html
Copyright © 2011-2022 走看看