zoukankan      html  css  js  c++  java
  • spring 事务配置方式以及事务的传播性、隔离级别

    在前面的文章中总结了spring事务的5中配置方式,但是很多方式都不用而且当时的配置使用的所有参数都是默认的参数,这篇文章就看常用的两种事务配置方式并信息配置事务的传播性、隔离级别、以及超时等问题,废话不说下面就来看看!

    一、注解式事务

    1、注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo。

    2、事务配置实例

    (1)、spring+mybatis 事务配置

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1.        <!-- 定义事务管理器 -->  
    2. <bean id="transactionManager"  
    3.     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    4.     <property name="dataSource" ref="dataSource" />  
    5. </bean>  
    6. <!--使用注释事务 -->  
    7. <tx:annotation-driven  transaction-manager="transactionManager" />  

    (2)、spring+hibernate 事务配置

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. <!-- 事务管理器配置,单数据源事务 -->  
    2. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
    3.     <property name="sessionFactory" ref="sessionFactory" />  
    4. </bean>  
    5.   
    6. <!-- 使用annotation定义事务 -->  
    7. <tx:annotation-driven  transaction-manager="transactionManager"   proxy-target-class="true" />  

    看到上面的这两段配置文件是不是很熟悉,对的这就是我们平时看到的事务的配置,在spring的配置中配置数据源即dataSource、事务管理器,事务管理器使用不同的orm框架事务管理器类就不同,比如这里使用的是mybatis 所以是

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. org.springframework.jdbc.datasource.DataSourceTransactionManager  

    如果使用hibernate 则事务管理器类为

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. org.springframework.orm.hibernate3.HibernateTransactionManager  

    这是使用注解方式时要配置的,代码中的具体的注解以及事务的传播性、隔离级别一般在service 层中配置下面看看

    3、@Transactional

    (1)、这里说明一下,有的把这个注解放在类名称上面了,这样你配置的这个@Transactional 对这个类中的所有public方法都起作用

    (2)、@Transactional 方法方法名上,只对这个方法有作用,同样必须是public的方法

    比如这里就对这个方法定义了一个事务同时设置了很多属性:

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,timeout=1,isolation=Isolation.DEFAULT)  
    2. public void saveUser(Map<String, String> map) throws Exception {  
    3.         System.out.println("方法开始");  
    4.     for (int i = 0; i < 500000; i++) {  
    5.             System.out.println("*");  
    6.         }  
    7.      System.out.println("进入保存");  
    8.      userDao.saveUser(map);  
    9.      System.out.println("退出保存");  
    10. }  

    4、事物配置中有哪些属性可以配置

    (1)、事务的传播性:@Transactional(propagation=Propagation.REQUIRED) 

          如果有事务, 那么加入事务, 没有的话新建一个(默认情况下)

    (2)、事务的超时性:@Transactional(timeout=30) //默认是30秒 

          注意这里说的是事务的超时性而不是Connection的超时性,这两个是有区别的

    (3)、事务的隔离级别:@Transactional(isolation = Isolation.READ_UNCOMMITTED)

          读取未提交数据(会出现脏读, 不可重复读) 基本不使用

    (4)、回滚:

    指定单一异常类:@Transactional(rollbackFor=RuntimeException.class)

    指定多个异常类:@Transactional(rollbackFor={RuntimeException.class, Exception.class})

    该属性用于设置需要进行回滚的异常类数组,当方法中抛出指定异常数组中的异常时,则进行事务回滚。

    (5)、只读:@Transactional(readOnly=true)

    该属性用于设置当前事务是否为只读事务,设置为true表示只读,false则表示可读写,默认值为false。

    ok 这种注解方式实现事务的配置以及一些属性的定义,其实事务的东西还有很多要注意的事项,如果要深入学习的话要学习的东西还很多,这里只是简单记录一下

    那我们要注意什么那:

    1、在spring配置文件中引入<tx:>命名空间:xmlns:tx="http://www.springframework.org/schema/tx"

                                     http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. <beans xmlns="http://www.springframework.org/schema/beans"  
    2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.      xmlns:tx="http://www.springframework.org/schema/tx"  
    4.     xmlns:context="http://www.springframework.org/schema/context"  
    5.     xmlns:aop="http://www.springframework.org/schema/aop"  
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
    7.                   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
    8.                   http://www.springframework.org/schema/context  
    9.                   http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    10.                   http://www.springframework.org/schema/aop   
    11.                   http://www.springframework.org/schema/aop/spring-aop.xsd        
    12.                  http://www.springframework.org/schema/tx   
    13.                  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  

    2、@Transactional 只能被应用到public方法上, 对于其它非public的方法,如果标记了@Transactional也不会报错,但方法没有事务功能.

    3、用 spring 事务管理器,由spring来负责数据库的打开,提交,回滚.默认遇到运行期例外(throw new RuntimeException("注释");)会回滚,即遇到不受检查(unchecked)的例外时回滚;而遇到需要捕获的例外(throw new Exception("注释");)不会回滚,即遇到受检查的例外(就是非运行时抛出的异常,编译器会检查到的异常叫受检查例外或说受检查异常)时,需我们指定方式来让事务回滚 要想所有异常都回滚,要加上 @Transactional( rollbackFor={Exception.class,其它异常}) .如果让unchecked例外不回滚: @Transactional(notRollbackFor=RunTimeException.class)
    如下:
    @Transactional(rollbackFor=Exception.class) //指定回滚,遇到异常Exception时回滚
    public void methodName() {
    throw new Exception("注释");

    }
    @Transactional(noRollbackFor=Exception.class)//指定不回滚,遇到运行期例外(throw new RuntimeException("注释");)会回滚
    public ItimDaoImpl getItemDaoImpl() {
    throw new RuntimeException("注释");
    }

    4、@Transactional 注解应该只被应用到 public 可见度的方法上。 如果你在 protected、private 或者 package-visible 的方法上使用 @Transactional 注解,它也不会报错, 但是这个被注解的方法将不会展示已配置的事务设置。


    5、@Transactional 注解可以被应用于接口定义和接口方法、类定义和类的 public 方法上。然而,请注意仅仅 @Transactional 注解的出现不足于开启事务行为,它仅仅 是一种元数据,能够被可以识别 @Transactional 注解和上述的配置适当的具有事务行为的beans所使用。上面的例子中,其实正是 <tx:annotation-driven/>元素的出现 开启 了事务行为。


    6、Spring团队的建议是你在具体的类(或类的方法)上使用 @Transactional 注解,而不要使用在类所要实现的任何接口上。你当然可以在接口上使用 @Transactional 注解,但是这将只能当你设置了基于接口的代理时它才生效。因为注解是 不能继承 的,这就意味着如果你正在使用基于类的代理时,那么事务的设置将不能被基于类的代理所识别,而且对象也将不会被事务代理所包装(将被确认为严重的)。因 此,请接受Spring团队的建议并且在具体的类上使用 @Transactional 注解。

    二、使用AOP的方式实现事务的配置

    1、这种事务使用也很多,下面我们来看看简单的例子

    2、事务配置实例:

    (1)、配置文件

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. <!-- 定义事务管理器 -->  
    2.     <bean id="transactionManager"  
    3.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
    4.         <property name="dataSource" ref="dataSource" />  
    5.     </bean>  
    6.     <!-- 下面使用aop切面的方式来实现 -->  
    7.     <tx:advice id="TestAdvice" transaction-manager="transactionManager">  
    8.         <!--配置事务传播性,隔离级别以及超时回滚等问题 -->  
    9.         <tx:attributes>  
    10.             <tx:method name="save*" propagation="REQUIRED" />  
    11.             <tx:method name="del*" propagation="REQUIRED" />  
    12.             <tx:method name="update*" propagation="REQUIRED" />  
    13.             <tx:method name="add*" propagation="REQUIRED" />  
    14.             <tx:method name="*" rollback-for="Exception" />  
    15.         </tx:attributes>  
    16.     </tx:advice>  
    17.     <aop:config>  
    18.         <!--配置事务切点 -->  
    19.         <aop:pointcut id="services"  
    20.             expression="execution(* com.website.service.*.*(..))" />  
    21.         <aop:advisor pointcut-ref="services" advice-ref="TestAdvice" />  
    22.     </aop:config>  

    上面我们看到了,简单的配置了事务,其中tx:attributes中设置了事务的传播性,隔离级别以及那种问题能进行回滚超时等这些问题,也就是你自己按照业务需求定制一个事务来满足你的业务需求。

    注意: 这里注意一下,在tx:method中配置了rollback_for 中配置的Exception 这个是运行时的异常才会回滚不然其他异常是不会回滚的!

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. @Service("userService")  
    2. public class UserService {  
    3.     @Autowired  
    4.     private UserDao userDao;  
    5.     public void saveUser(Map<String, String> map) throws Exception {  
    6.          userDao.saveUser(map);  
    7.          throw new RuntimeException();  
    8.          // throw new Exception ();  
    9.            
    10.     }  
    11. }  


    这里我们看到了,如果抛出的是一个运行时异常则会回滚而如果抛出的不是运行时异常则会不回滚的。

    需要注意的地方:

    (1)、在spring 配置文件中引入:xmlns:aop="http://www.springframework.org/schema/aop"

     http://www.springframework.org/schema/aop 
                      http://www.springframework.org/schema/aop/spring-aop.xsd

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. <beans xmlns="http://www.springframework.org/schema/beans"  
    2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.      xmlns:tx="http://www.springframework.org/schema/tx"  
    4.     xmlns:context="http://www.springframework.org/schema/context"  
    5.     xmlns:aop="http://www.springframework.org/schema/aop"  
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
    7.                   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
    8.                   http://www.springframework.org/schema/context  
    9.                   http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    10.                   http://www.springframework.org/schema/aop   
    11.                   http://www.springframework.org/schema/aop/spring-aop.xsd        
    12.                  http://www.springframework.org/schema/tx   
    13.                  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  

    (2) advice(建议)的命名:由于每个模块都会有自己的Advice,所以在命名上需要作出规范,初步的构想就是模块名+Advice(只是一种命名规范)。

    (3) tx:attribute标签所配置的是作为事务的方法的命名类型。

             如<tx:method name="save*" propagation="REQUIRED"/>

            其中*为通配符,即代表以save为开头的所有方法,即表示符合此命名规则的方法作为一个事务。

            propagation="REQUIRED"代表支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

    (4) aop:pointcut标签配置参与事务的类,由于是在Service中进行数据库业务操作,配的应该是包含那些作为事务的方法的Service类。

           首先应该特别注意的是id的命名,同样由于每个模块都有自己事务切面,所以我觉得初步的命名规则因为 all+模块名+ServiceMethod。而且每个模块之间不同之处还在于以下一句:

           expression="execution(* com.test.testAda.test.model.service.*.*(..))"

           其中第一个*代表返回值,第二*代表service下子包,第三个*代表方法名,“(..)”代表方法参数。

    (5) aop:advisor标签就是把上面我们所配置的事务管理两部分属性整合起来作为整个事务管理。

    (6)注意标红的地方

    ok 到这里两种配置方式简单的demo 都有了,下面我们来看看tx:method 中还有那些属性可以配置

    下面来看看aop 这种方式来配置的时候我们还能配置那些属性:

    <tx:advice id="advice" transaction-manager="txManager">
      <tx:attributes>
        <!-- tx:method的属性:
              * name 是必须的,表示与事务属性关联的方法名(业务方法名),对切入点进行细化。通配符(*)可以用来指定一批关联到相同的事务属性的方法。
                        如:'get*'、'handle*'、'on*Event'等等.
              * propagation  不是必须的 ,默认值是REQUIRED 
                                表示事务传播行为, 包括REQUIRED,SUPPORTS,MANDATORY,REQUIRES_NEW,NOT_SUPPORTED,NEVER,NESTED
              * isolation    不是必须的 默认值DEFAULT 
                                表示事务隔离级别(数据库的隔离级别) 
              * timeout      不是必须的 默认值-1(永不超时)
                                表示事务超时的时间(以秒为单位) 
              
              * read-only    不是必须的 默认值false不是只读的 
                                表示事务是否只读? 
              
              * rollback-for 不是必须的   
                                表示将被触发进行回滚的 Exception(s);以逗号分开。
                                如:'com.foo.MyBusinessException,ServletException' 
              
              * no-rollback-for 不是必须的  
                                  表示不被触发进行回滚的 Exception(s);以逗号分开。
                                  如:'com.foo.MyBusinessException,ServletException'
                                  
                                  
            任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚                      
         -->
         <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
         <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
         <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
         <!-- 其他的方法之只读的 -->
         <tx:method name="*" read-only="true"/>
      </tx:attributes>
    </tx:advice>



    OK 两种配置方式我们也都简单学习了,两种配置方式有哪些属性要配置我们也了解了,但是,这里只是说了有这两种常用的方法,而没有具体说事务以及事务的配置带来的数据的安全性以及性能的影响,其实事务不是那么简单,具体深入学习希望以后有总结!

  • 相关阅读:
    解决pyinstaller 打包后运行exe程序出现的"ModuleNotFindError"
    【Tensorflow专题-03】深层神经网络
    【Tensorflow专题-02】使用tensorflow训练神经网络
    【Tensorflow专题-01】深度学习应用介绍及tensorflow基本使用
    Github 简明教程
    Composer 安装与使用
    为jQuery的$.ajax设置超时时间
    thinkphp 数据库表多字段排序
    TP5如何隐藏入口文件index.php
    Atom编辑器快捷键大全
  • 原文地址:https://www.cnblogs.com/lizhonghua34/p/6148854.html
Copyright © 2011-2022 走看看