• spring+mybatis事务配置(转载)


    原文地址:http://blog.csdn.net/wgh1015398431/article/details/52861048

    申明式事务配置步骤

    1xml文件头部需要添加spring的相关支持:

    [html] view plain copy

    1. <beans xmlns="http://www.springframework.org/schema/beans"  
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3.     xmlns:context="http://www.springframework.org/schema/context"  
    4.     xmlns:aop="http://www.springframework.org/schema/aop"  
    5.     xmlns:tx="http://www.springframework.org/schema/tx"  
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans   
    7. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
    8.         http://www.springframework.org/schema/context  
    9.         http://www.springframework.org/schema/context/spring-context-3.1.xsd  
    10.         http://www.springframework.org/schema/tx  
    11.         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
    12.         http://www.springframework.org/schema/aop   
    13.         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">  


    2
    、配置事务管理

    [html] view plain copy

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

     

    3、配置需要加入事务的方法规则,或者说是一个切面

    [html] view plain copy

    1. <!-- 事务相关控制配置:例如配置事务的传播机制 -->  
    2.     <tx:advice id="iccardTxAdvice" transaction-manager="transactionManager">  
    3.         <tx:attributes>  
    4.           <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>  
    5.           <tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />  
    6.           <tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />  
    7.           <tx:method name="create*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" />  
    8.           <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />  
    9.               
    10.           <tx:method name="find*" propagation="SUPPORTS" />  
    11.           <tx:method name="get*" propagation="SUPPORTS" />  
    12.           <tx:method name="select*" propagation="SUPPORTS" />  
    13.           <tx:method name="query*" propagation="SUPPORTS" />  
    14.         </tx:attributes>  
    15.     </tx:advice>  
    16.         
    17.     <!-- 把事务控制在service -->  
    18.     <aop:config>      
    19.         <aop:pointcut id="iccardTerm" expression="execution(public * com.shfft.iccardterm.service.*.*(..))" />  
    20.         <aop:advisor pointcut-ref="iccardTerm" advice-ref="iccardTxAdvice" />  
    21.     </aop:config>  

     

    注解式事务配置

    1、添加注解配置

    [html] view plain copy

    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、在需要加入事务的方法或者类上添加@Transactional

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

    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

    例如:

    [html] view plain copy

    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. }    

     

    解释说明

    事务的传播级别定义的是事务的控制范围,主要是父子事务之间的相互影响关系;事务的隔离级别定义的是事务读写的控制范围,主要是两个事务之间的相互影响关系。

    传播级别:

    1)、REQUIRED

    如果当前方法已经在事务中,那么就以父事务执行,不需要新建事务;如果当前方法不在事务中,那么就为当前方法新建事务。回滚情况:父子方法中任何地方出现问题,都会全部回滚。

    2)、SURPPORTED

    如果当前方法已经在事务中,那么就以当前事务执行;如果当前方法不再事务中,那么就以非事务方式运行。如果运行在事务中,那么只要出现异常都会回滚。

    3)、NOT_SURPPORTED

    如果当前方法已经在事务中,那么就挂起当前事务,以非事务方式运行,方法执行完毕后,恢复事务;如果当前方法不再事务中,那么就以非事务方式执行。

    4)、MANDATORY

    强制以事务方式执行,如果当前方法不在事务中,那么会抛出异常。

    5)、NEVER

    MANDATORY相反,强制以非事务方式执行,如果当前方法在事务中,那么会抛出异常。

    6)、REQUIRED_NEW

    REQUIRED不同的是,无论该方法当前是不是在事务中,都会为自己新建一个事务。如果当前已经在事务中,那么会挂起父事务,为自己新建一个事务。父事务不影响它子事务的提交和回滚。

    7)、NESTED

    嵌套事务。理解Nested的关键是savepoint。他与PROPAGATION_REQUIRES_NEW的区别是,PROPAGATION_REQUIRES_NEW另起一个事务,将会与他的父事务相互独立,而Nested的事务和他的父事务是相依的,他的提交是要等和他的父事务一块提交的。也就是说,如果父事务最后回滚,他也要回滚的。而Nested事务的好处是他有一个savepoint

    例如:

    [html] view plain copy

    1. ServiceA {  
    2. /**  
    3. 事务属性配置为 PROPAGATION_REQUIRED  
    4. */  
    5. void methodA() {  
    6. try {  
    7. //savepoint  
    8. ServiceB.methodB(); //PROPAGATION_NESTED 级别  
    9. } catch (SomeException) {  
    10. // 执行其他业务 ServiceC.methodC();  
    11. }  
    12. }  
    13. }  


    也就是说ServiceB.methodB失败回滚,那么ServiceA.methodA也会回滚到savepoint点上,ServiceA.methodA可以选择另外一个分支,比如ServiceC.methodC,继续执行,来尝试完成自己的事务。

  • 相关阅读:
    Python机器学习笔记:使用sklearn做特征工程和数据挖掘
    Python numpy中矩阵的用法总结
    Python机器学习笔记:K-近邻(KNN)算法
    Python机器学习笔记:Logistic Regression
    python机器学习笔记:ID3决策树算法实战
    Mysql 多表查询详解
    JavaScript cookie操作实现点赞功能
    js操作cookie
    【IntelliJ IDEA】idea上提交代码到GitHub,已经提交了 但是GitHub上却没有的解决办法
    Mybatis分页插件PageHelper的配置和使用方法
  • 原文地址:https://www.cnblogs.com/xiaolang8762400/p/7018038.html
Copyright © 2020-2023  润新知