事务(Transaction)在应用程序中起着至关重要的作用:它是一系列任务的组成工作单元,在这个工作单元中,所有的任务必须同时执行。它们只有二种可能执行结果,要么所有任务全部执行成功,要么所有任务全部执行失败。如数据库管理中的汇款业务,业务包括了汇款人的账户的汇出操作、被汇款人的汇入操作,为了保持一致性,这两个操作必须同时执行成功。
数据库事务的ACID特性
1. Atomic(原子性)
事务必须是原子的,在事务结束的时候,事务中的所有任务必须全部成功完成,否则全部失败,事务回滚到事务开始之间的状态。
2. Consistency(一致性)
数据库中的所有数据和现实(业务逻辑)保持一致。如果事务失败数据必须返回到事务执行之前的状态,反之修改数据和现实的同步。
3. Isolation(隔离性)
隔离性是事务与事务之间的屏障,每个事务必须与其他事务的执行结果隔离开,直到该事务执行完毕,它保证了事务的访问的任何数据不会受其他事务执行结果的影响。
4. Durability(持久性)
如果事务成功执行,无论系统发生任何情况,事务的持久性都必须保证事务的执行结果是永久的。
事务之间的缺陷
在事务处理中有违返ACID特性的3个问题:脏读取,不可重复读和幻读行。如果存在多个并发事务在运行,而这种事务操作了同一个数据来完成它们的任务,就会导致3个问题的存生。要解决它们,就必须在事务之间定义合适的隔离级别。
危害程度依次为:脏读取最大-->不可重复读取-->幻影行最小。
1. 脏读取
当一个事务读取了另一个事务尚未提交的更新,就叫脏读取。在另一个事务回滚的情况下,当前事务所读取的另一个事务的数据就是无效的。
2. 不可重复读取
在一个事务中执行多次同样的查询操作,但每次查询的结果都不一样,就叫做不可重复读取,通常这种情况是由于数据在二次查询之间被另一个并发的事务所修改。
3. 幻影行
这是对事务危害最小的一个问候,它类似不可重复读取,也是一个事务的更新结果影响到另一个事务问题。但是它不仅影响另一个事务查询结果,而且还会使查询语句返回一些不同的行录行。
事务的属性
1. 事务的传播
传播行为是事务应用于方法的边界,它定义了事务的建立,暂停等行为属性。
在Spring中共有7种,EJB CMT共6种。
*PROPAGATION_MANDATORY: 规定了方法必须在事务中运行,否则会抛出异常
*PROPAGATION_NESTED: 使方法运行在嵌套事务中,否则这个属性和PROPAGATION_REQUIRED属性的义相同
*PROPAGATION_NEVER :使当前方法永远不在事务中运行,否则抛出异常
*PROPAGATION_NOT_SUPPORTED :定义为当前事务不支持的方法,在该方法运行期间正在运行的事务会被暂停
*PROPAGATION_REQUIRED :规定当前的方法必须在事务中,如果没有事务就创建一个新事务,一个新事务和方法一同开始,随着方法的返回或抛出异常而终止
*PROPAGATION_REQUIRED_NEW :当前方法必须创建新的事务来运行,如果现存的事务正在运行就暂停它
*PROPAGATION_SUPPORTS :规定当前方法支持当前事务处理,但如果没有事务在运行就使用非事务方法执行
以上定义Spring在事务中的传播行为分别对应EJB的事务CMT中的所有传播行为,其在PROPAGATION_NESTED是Spring在CMT之外定义的事务传播行为。
例如:
<property name="transactionAttributes"> <props> <prop key="query*">PROPAGATION_REQUIRED,timeout_5,readOnly</prop> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> </props> </property>
2. 事务的隔离级别
为解决事务之间的3个缺陷,必须在事务之间建立隔离关系来保证事务的完整性。
ISOLATION_DEFAULT
使用数据库默认的隔离级别
ISOLATION_COMMITTED
允许读取其他并发事务已经提交的更新(防此脏读)
ISOLATION_READ_UNCOMMITTED
允许读取其他并发事务还未提交的更新,会导致事务之 间的3个缺陷发生,这是速度最快的一个隔离级别,但同 时它的隔离级别也是最低
ISOLATION_REPEATABLE_READ
除非事务自身修改了数据,否则规定事务多次重复读取 数据必须相同(防此脏读,不可重复读)
ISOLATION_SERIALIZABLE
这是最高的隔离级别,它可以防此脏读,不可重复读和 幻读等问题,但因其侵占式的数据记录完全锁定,导致
它影响事务的性能,成为隔离级别中最展慢的一个。
注意:并不是所有的资源管理器都支持所有的隔离级别,可针对不同的资源管理使用以上的隔离级别。
3. 事务的只读属性
在对数据库的操作中,查询是使用最频繁的操作,每次执行查询时都要从数据库中重新读取数据,有时多次读取的数据都是相同的,这样的数据操作不仅浪费了系统资源,还影响了系统速度。对访问量大的程序来说,节省这部分资源可以大大提 升系统速度。
如果将事务声明为只读的,那么数据库可以根据事务的特性优化事务的读取操作。事务的只读属性需要配合事务的传播行为共同设置。例如:
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
4. 事务的超时属性
这个属性和事务的只读属性一样需要搭配事务的传播行为共同设置,它设置了事务的超时时间,事务本身可能会因某种原因很长没有回应,在这期间事务可能锁定 了数据库的表格,这样会出现严重的性能问题。通过设置事务的超时时间,从开始执行事务起,在规定的超时时间内如果没有事务就将它回滚。事务的超时属性以 timeout_为前缀和一个整型数字定义,例如:
<prop key="query*">PROPAGATION_REGUIRED,timeout_5,readOnly</prop>
Spring的事务
分类
1. 声明式事务
无需要去处理获得连接、关闭连接、事务提交和回滚等这些操作。再也无需要我们在与事务相关的方法中处理大量的try…catch…finally代码。
2. 编程式事务
是通过编码方式实现事务,即类似于JDBC编程实现事务管理。
3. 配置方式
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。
DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问 时,DataSource实际为SessionFactory,TransactionManager的实现为 HibernateTransactionManager。
事务管理器
Spring的事务管理器有5个,都实现了PlatformTransactionManager接口,如下所示:
DataSourceTransactionManager JDBC事务管理器 (编程式)
HibernateTransactionManager Hibernate事务管理器
JdoTransactionManager JDO事务管理器
JtaTransactionManager JTA事务管理器 (编程式)
PersistenceBrokerTransactionManager Apache的OJB事务管理器
1、使用事务代理工厂(JDBC类型)
事务代理工厂TransactionProxyFactoryBean包括了事务拦截器,目标代理和事务的属性设置,它配置方便,使用灵活,在声明式事务中广泛使用。
使用TransactionProxyFactoryBean需要注入它所依赖的事务管理器,设置代理的目标对象,代理对象的生成方式和事务属性。代理对象是在目标对象上生成的包括事物和AOP切面的新对象,这个新的对象用来替代目标对象以支持事物或AOP提供的切面功能。
其中对象代理的生成方式可根据CGLIB生成代理,例如:
<property name="proxyTargetClass" value="true"/>
最后再介绍一个TransactionProxyFactoryBean中如何设置事务的属性。 TransactionProxyFactoryBean的transactionAttributes属性用于指定事务的属性,它是一个 Properties类型的属性集合,以方法名和事务属性组成键值对,给不同的方法指定不同的事务属性。例如:
<prop key="query*">PROPAGATION_REQUIRED,timeout_5,readOnly</prop>
方法名作为声明时,可以使用*通配符,上述代码定义对所有以query作前缀的方法,并在定义的事务中完成操作,事务的不同属性之间以","分隔。
通过一个实例介绍如何使用TransactionProxyFactoryBean完全成Spring的声明式事务管理。
**************************************************************
<!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>org.gjt.mm.mysql.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/数据库Schema</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>admin</value> </property> </bean> <!-- 配置JDBC事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <!-- 配置事件代理工厂 --> <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <!-- 要依赖事务管理器 --> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <!-- 要依赖目标对象 --> <property name="target"> <bean id="userDAO" class="tarena.dao.UserDAO"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> </property> <!-- 要依赖代理方式 --> <property name="proxyTargetClass" value="true"></property> <!-- 要依赖事务属性 --> <property name="transactionAttributes"> <props> <propkey="query*">PROPAGATION_REQUIRED,timeout_1,readOnly</prop> <prop key="insert*">PROPAGATION_REQUIRED,timeout_2</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> </props> </property> </bean>
如果timeout_0,就会抛出事务异常,意思是查询操作超过0秒便会抛出异常并结束事务。
2、每个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>
3、所有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>
4、使用拦截器
<?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>
5、使用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>
6、全注解
<?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>
此时在DAO上需加上@Transactional注解,如下:
package com.bluesky.spring.dao;
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();
}
}
来源
Spring事务管理:http://blog.youkuaiyun.com/daoma/article/details/2302245
事务配置的五种式(声明式):http://www.blogjava.net/robbie/archive/2009/04/05/264003.html
编程式事务:http://kingj.iteye.com/blog/1461558
Spring事务配置的五种方式:http://www.blogjava.net/robbie/archive/2009/04/05/264003.html