Spring 事物

事务首先是一系列操作组成的工作单元,该工作单元内的操作是不可分割的,即要么所有操作都做,要么所有操作都不做,这就是事务。
事务必需满足ACID(原子性、一致性、隔离性和持久性)特性,缺一不可:

  • 原子性(Atomicity) : 即事务是不可分割的最小工作单元,事务内的操作要么全做,要么全不做;
  • 一致性(Consistency) : 在事务执行前数据库的数据处于正确的状态,而事务执行完成后数据库的数据还是处于正确的状态,即数据完整性约束没有被破坏;如银行转帐,A转帐给B,必须保证A的钱一定转给B,一定不会出现A的钱转了但B没收到,否则数据库的数据就处于不一致(不正确)的状态。
  • 隔离性(Isolation) : 并发事务执行之间无影响,在一个事务内部的操作对其他事务是不产生影响,这需要事务隔离级别来指定隔离性;
  • 持久性(Durability) : 事务一旦执行成功,它对数据库的数据的改变必须是永久的,不会因比如遇到系统故障或断电造成数据不一致或丢失。

Spring 事务管理器

事务的核心接口

Spring并不直接管理事务,而是提供了多种事务管理器,他们将事务管理的职责委托给Hibernate或者JTA等持久化机制所提供的相关平台框架的事务来实现。

Public interface PlatformTransactionManager()...{  
    // 由TransactionDefinition得到TransactionStatus对象
    TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException; 
    // 提交
    Void commit(TransactionStatus status) throws TransactionException;  
    // 回滚
    Void rollback(TransactionStatus status) throws TransactionException;  
    } 
JDBC事务(DataSourceTransactionManager)

DataSourceTransactionManager是通过调用java.sql.Connection来管理事务,而后者是通过DataSource获取到的。通过调用连接的commit()方法来提交事务,同样,事务失败则通过调用rollback()方法进行回滚。

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
Hibernate事务(HibernateTransactionManager)

事务管理的职责委托给org.hibernate.Transaction对象,而后者是从Hibernate Session中获取到的。当事务成功完成时,HibernateTransactionManager将会调用Transaction对象的commit()方法,反之,将会调用rollback()方法。

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
Java持久化API事务(JpaTransactionManager)

JpaTransactionManager只需要装配一个JPA实体管理工厂(javax.persistence.EntityManagerFactory接口的任意实现)。JpaTransactionManager将与由工厂所产生的JPA EntityManager合作来构建事务。

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
Java原生API事务(JtaTransactionManager)

JtaTransactionManager将事务管理的责任委托给javax.transaction.UserTransaction和javax.transaction.TransactionManager对象,其中事务成功完成通过UserTransaction.commit()方法提交,事务失败通过UserTransaction.rollback()方法回滚。

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManagerName" value="java:/TransactionManager" />
    </bean>

事务属性

传播行为
Required传播行为Required传播行为抛出异常情况

PROPAGATION_REQUIRED : 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

Required+Supports传播行为Supports+Supports传播行为

PROPAGATION_SUPPORTS : 支持当前事务,如果当前没有事务,就以非事务方式执行。

Required+Mandatory传播行为Supports+Mandatory传播行为

- PROPAGATION_MANDATORY : 支持当前事务,如果当前没有事务,就抛出异常。

RequiresNew传播行为RequiresNew传播行为并抛出异常

PROPAGATION_REQUIRES_NEW : 新建事务,如果当前存在事务,把当前事务挂起。

Required+NotSupported传播行为Supports+NotSupported传播行为

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

Required+Never传播行为Supports+Never传播行为

PROPAGATION_NEVER : 以非事务方式执行,如果当前存在事务,则抛出异常。

Required+Nested传播行为Nested+Nested传播行为

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

隔离规则

并发事务引起的问题

  • 脏读(Dirty reads) : 脏读发生在一个事务读取了另一个事务改写但尚未提交的数据时。如果改写在稍后被回滚了,那么第一个事务获取的数据就是无效的。
  • 不可重复读(Nonrepeatable read) : 不可重复读发生在一个事务执行相同的查询两次或两次以上,但是每次都得到不同的数据时。这通常是因为另一个并发事务在两次查询期间进行了更新。
  • 幻读(Phantom read) : 幻读与不可重复读类似。它发生在一个事务(T1)读取了几行数据,接着另一个并发事务(T2)插入了一些数据时。在随后的查询中,第一个事务(T1)就会发现多了一些原本不存在的记录。

隔离级别

  • ISOLATION_DEFAULT : 使用后端数据库默认的隔离级别
  • ISOLATION_READ_UNCOMMITTED : 最低的隔离级别,允许读取尚未提交的数据变更,可能会导致脏读、幻读或不可重复读。
  • ISOLATION_READ_COMMITTED : 允许读取并发事务已经提交的数据,可以阻止脏读,但是幻读或不可重复读仍有可能发生。
  • ISOLATION_REPEATABLE_READ : 对同一字段的多次读取结果都是一致的,除非数据是被本身事务自己所修改,可以阻止脏读和不可重复读,但幻读仍有可能发生。
  • ISOLATION_SERIALIZABLE : 最高的隔离级别,完全服从ACID的隔离级别,确保阻止脏读、不可重复读以及幻读,也是最慢的事务隔离级别,因为它通常是通过完全锁定事务相关的数据库表来实现的。
只读

如果事务只对后端的数据库进行该操作,数据库可以利用事务的只读特性来进行一些特定的优化。通过将事务设置为只读,你就可以给数据库一个机会,让它应用它认为合适的优化措施。

事务超时

为了使应用程序很好地运行,事务不能运行太长的时间。因为事务可能涉及对后端数据库的锁定,所以长时间的事务会不必要的占用数据库资源。事务超时就是事务的一个定时器,在特定时间内事务如果没有执行完毕,那么就会自动回滚,而不是一直等待其结束。

回滚规则

默认情况下,事务只有遇到运行期异常时才会回滚,而在遇到检查型异常时不会回滚(这一行为与EJB的回滚行为是一致的) .但是你可以声明事务在遇到特定的检查型异常时像遇到运行期异常那样回滚。同样,你还可以声明事务遇到特定的异常不回滚,即使这些异常是运行期异常。

编程式事务

编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理。
Spring提供两种方式的编程式事务管理,分别是:使用TransactionTemplate和直接使用PlatformTransactionManager。

使用TransactionTemplate

采用TransactionTemplate和采用其他Spring模板,如JdbcTempalte和HibernateTemplate是一样的方法。它使用回调方法,把应用程序从处理取得和释放资源中解脱出来。如同其他模板,TransactionTemplate是线程安全的。

  TransactionTemplate tt = new TransactionTemplate(); // 新建一个TransactionTemplate
    Object result = tt.execute(
        new TransactionCallback(){  
            public Object doTransaction(TransactionStatus status){  
                updateOperation();  
                return resultOfUpdateOperation();  
            }  
    }); // 执行execute方法进行事务管理
使用PlatformTransactionManager
 DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); //定义一个某个框架平台的TransactionManager,如JDBC、Hibernate
    dataSourceTransactionManager.setDataSource(this.getJdbcTemplate().getDataSource()); // 设置数据源
    DefaultTransactionDefinition transDef = new DefaultTransactionDefinition(); // 定义事务属性
    transDef.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED); // 设置传播行为属性
    TransactionStatus status = dataSourceTransactionManager.getTransaction(transDef); // 获得事务状态
    try {
        // 数据库操作
        dataSourceTransactionManager.commit(status);// 提交
    } catch (Exception e) {
        dataSourceTransactionManager.rollback(status);// 回滚
    }

声明式事务

每个Bean都有一个代理
< ?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    < bean id="sessionFactory"        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        < property name="configLocation" value="classpath:hibernate.cfg.xml" />  
        < property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    < /bean>  
    < !-- 定义事务管理器(声明式的事务) -->  
    < bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        < property name="sessionFactory" ref="sessionFactory" />
    </bean>
    < !-- 配置DAO -->
    < bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
        < property name="sessionFactory" ref="sessionFactory" />
    < /bean>
    < bean id="userDao"      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
           <!-- 配置事务管理器 -->  
           <property name="transactionManager" ref="transactionManager" />     
        <property name="target" ref="userDaoTarget" />  
         <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
        <!-- 配置事务属性 -->  
        <property name="transactionAttributes">  
            <props>  
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>  
        </property>  
    </bean>  
</beans>
所有Bean共享一个代理基类
< ?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:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    < bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        < property name="configLocation" value="classpath:hibernate.cfg.xml" />  
        < property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    < /bean>  
    < !-- 定义事务管理器(声明式的事务) -->  
    < bean id="transactionManager"
 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        < property name="sessionFactory" ref="sessionFactory" />
    < /bean>
    < bean id="transactionBase"  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"   lazy-init="true" abstract="true">  
        <!-- 配置事务管理器 -->  
        <property name="transactionManager" ref="transactionManager" />  
        <!-- 配置事务属性 -->  
        <property name="transactionAttributes">  
            <props>  
                <prop key="*">PROPAGATION_REQUIRED</prop>  
            </props>  
        </property>  
    </bean>    
    <!-- 配置DAO -->
    <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="userDao" parent="transactionBase" >  
        <property name="target" ref="userDaoTarget" />   
    </bean>
</beans>
使用拦截器
< ?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    < bean id="sessionFactory"  
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        < property name="configLocation" value="classpath:hibernate.cfg.xml" />  
        < property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    < /bean>  
    < !-- 定义事务管理器(声明式的事务) -->  
    < bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        < property name="sessionFactory" ref="sessionFactory" />
    < /bean> 
    < bean id="transactionInterceptor"     class="org.springframework.transaction.interceptor.TransactionInterceptor">  
        < property name="transactionManager" ref="transactionManager" />  
        < !-- 配置事务属性 -->  
        < property name="transactionAttributes">  
            < props>  
                < prop key="*">PROPAGATION_REQUIRED</prop>  
            < /props>  
        < /property>  
    < /bean>
    < bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
        < property name="beanNames">  
            < list>  
                < value>*Dao</value>
            < /list>  
        < /property>  
        < property name="interceptorNames">  
            < list>  
                < value>transactionInterceptor</value>  
            < /list>  
        < /property>  
    < /bean>  
    < !-- 配置DAO -->
    < bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
        < property name="sessionFactory" ref="sessionFactory" />
    < /bean>
< /beans>
使用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:context="http://www.springframework.org/schema/context"
    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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  < context:annotation-config />
    < context:component-scan base-package="com.bluesky" />
    < bean id="sessionFactory"           class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        < property name="configLocation" value="classpath:hibernate.cfg.xml" />  
        < property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    < /bean>  
    < !-- 定义事务管理器(声明式的事务) -->  
    < bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        < property name="sessionFactory" ref="sessionFactory" />
    < /bean>
    < tx:advice id="txAdvice" transaction-manager="transactionManager">
        < tx:attributes>
            < tx:method name="*" propagation="REQUIRED" />
        < /tx:attributes>
    < /tx:advice>
    < aop:config>
        < aop:pointcut id="interceptorPointCuts"
            expression="execution(* com.bluesky.spring.dao.*.*(..))" />
        < aop:advisor advice-ref="txAdvice"
            pointcut-ref="interceptorPointCuts" />        
    < /aop:config>      
< /beans>
使用全注解
< ?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:context="http://www.springframework.org/schema/context"
    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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    < context:annotation-config />
    < context:component-scan base-package="com.bluesky" />
    < tx:annotation-driven transaction-manager="transactionManager"/>
    < bean id="sessionFactory"  
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        < property name="configLocation" value="classpath:hibernate.cfg.xml" />  
        < property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    < /bean>  
    < !-- 定义事务管理器(声明式的事务) -->  
    < bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        < property name="sessionFactory" ref="sessionFactory" />
    < /bean>
< /beans>
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import com.bluesky.spring.domain.User;
@Transactional
@Component("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
    public List<User> listUsers() {
        return this.getSession().createQuery("from User").list();
    }
}

@Transactional配置详解

Spring提供的用于开启对注解事务管理的支持,从而能识别Bean类上的@Transactional注解元数据,其具有以下属性:

  • transaction-manager : 指定事务管理器名字,默认为transactionManager,当使用其他名字时需要明确指定;
  • proxy-target-class : 表示将使用的代码机制,默认false表示使用JDK代理,如果为true将使用CGLIB代理;
  • order : 定义事务通知顺序,默认Ordered.LOWEST_PRECEDENCE,表示将顺序决定权交给AOP来处理。;

Spring使用@Transaction来指定事务属性,可以在接口、类或方法上指定,如果类和方法上都指定了@Transaction,则方法上的事务属性被优先使用,具体属性如下:

  • value : 指定事务管理器名字,默认使用指定的事务管理器,用于支持多事务管理器环境;
  • propagation : 指定事务传播行为,默认为Required,使用Propagation.REQUIRED指定;
  • isolation : 指定事务隔离级别,默认为“DEFAULT”,使用Isolation.DEFAULT指定;
  • readOnly : 指定事务是否只读,默认false表示事务非只读;
  • timeout : 指定事务超时时间,以秒为单位,默认-1表示事务超时将依赖于底层事务系统;
  • rollbackFor : 指定一组异常类,遇到该类异常将回滚事务;
  • rollbackForClassname : 指定一组异常类名字,其含义与中的rollback-for属性语义完全一样;
  • noRollbackFor : 指定一组异常类,即使遇到该类异常也将提交事务,即不回滚事务;
  • noRollbackForClassname : 指定一组异常类名字,其含义与中的no-rollback-for属性语义完全一样;

使用@Transactional注解事务管理需要特别注意以下几点::

  • 如果在接口、实现类或方法上都指定了@Transactional 注解,则优先级顺序为方法>实现类>接口;
  • 建议只在实现类或实现类的方法上使用@Transactional,而不要在接口上使用,这是因为如果使用JDK代理机制是没问题,因为其使用基于接口的代理;而使用使用CGLIB代理机制时就会遇到问题,因为其使用基于类的代理而不是接口,这是因为接口上的@Transactional注解是“不能继承的”;
  • 在Spring代理机制下(不管是JDK动态代理还是CGLIB代理),“自我调用”同样不会应用相应的事务属性,其语义和中一样;
  • 默认只对RuntimeException异常回滚;
  • 在使用Spring代理时,默认只有在public可见度的方法的@Transactional 注解才是有效的,其它可见度(protected、private、包可见)的方法上即使有@Transactional 注解也不会应用这些事务属性的,Spring也不会报错,如果你非要使用非公共方法注解事务管理的话,可考虑使用AspectJ。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值