1、Spring聲明式事務(wù)配置的五種方式

  前段時(shí)間對(duì)Spring的事務(wù)配置做了比較深入的研究,在此之前對(duì)Spring的事務(wù)配置雖說也配置過,但是一直沒有一個(gè)清楚的認(rèn)識(shí)。通過這次的學(xué)習(xí)發(fā)覺Spring的事務(wù)配置只要把思路理清,還是比較好掌握的。

  總結(jié)如下:

  Spring配置文件中關(guān)于事務(wù)配置總是由三個(gè)組成部分,分別是DataSource、TransactionManager和代理機(jī)制這三部分,無論哪種配置方式,一般變化的只是代理機(jī)制這部分。

  DataSource、TransactionManager這兩部分只是會(huì)根據(jù)數(shù)據(jù)訪問方式有所變化,比如使用Hibernate進(jìn)行數(shù)據(jù)訪問時(shí),DataSource實(shí)際為SessionFactory,TransactionManager的實(shí)現(xiàn)為HibernateTransactionManager。

  具體如下圖:

Spring事務(wù)配置 (2)

  根據(jù)代理機(jī)制的不同,總結(jié)了五種Spring事務(wù)的配置方式,配置文件如下:

第一種方式:每個(gè)Bean都有一個(gè)代理

<?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>
<!-- 定義事務(wù)管理器(聲明式的事務(wù)) -->
<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">
<!-- 配置事務(wù)管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="userDaoTarget" />
<property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
<!-- 配置事務(wù)屬性 -->
<property name="transactionAttributes"> <props>
<prop key="*">PROPAGATION_REQUIRED</prop> </props>
</property>
</bean>
</beans>

第二種方式:所有Bean共享一個(gè)代理基類

 <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>
<!-- 定義事務(wù)管理器(聲明式的事務(wù)) -->
<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"> <!-- 配置事務(wù)管理器 -->
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事務(wù)屬性 -->
<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>

第三種方式:使用攔截器

......     <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>
<!-- 定義事務(wù)管理器(聲明式的事務(wù)) -->
<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" />
<!-- 配置事務(wù)屬性 -->
<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>

第四種方式:使用tx標(biāo)簽配置的攔截器

<?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>
<!-- 定義事務(wù)管理器(聲明式的事務(wù)) -->
<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>
</bean>

第五種方式:全注解

......     <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> <!-- 定義事務(wù)管理器(聲明式的事務(wù)) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

  此時(shí)在DAO上需加上@Transactional注解,如下:

@Transactional @Component("userDao") public class UserDaoImpl extends HibernateDaoSupport implements UserDao {     public List<User> listUsers() {         return this.getSession().createQuery("from User").list();     }     ...... }

  注:以上部分內(nèi)容轉(zhuǎn)載自http://www.aygfsteel.com/robbie.html

2、事務(wù)的傳播屬性(Propagation)

  Propagation :key屬性確定代理應(yīng)該給哪個(gè)方法增加事務(wù)行為。這樣的屬性最重要的部份是傳播行為。有以下選項(xiàng)可供使用:PROPAGATION_REQUIRED--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就新建一個(gè)事務(wù)。這是最常見的選擇。

  PROPAGATION_REQUIRED--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就新建一個(gè)事務(wù)。這是最常見的選擇。
  PROPAGATION_SUPPORTS--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就以非事務(wù)方式執(zhí)行。
  PROPAGATION_MANDATORY--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就拋出異常。
  PROPAGATION_REQUIRES_NEW--新建事務(wù),如果當(dāng)前存在事務(wù),把當(dāng)前事務(wù)掛起。
  PROPAGATION_NOT_SUPPORTED--以非事務(wù)方式執(zhí)行操作,如果當(dāng)前存在事務(wù),就把當(dāng)前事務(wù)掛起。
  PROPAGATION_NEVER--以非事務(wù)方式執(zhí)行,如果當(dāng)前存在事務(wù),則拋出異常。

  1: PROPAGATION_REQUIRED
  加入當(dāng)前正要執(zhí)行的事務(wù)不在另外一個(gè)事務(wù)里,那么就起一個(gè)新的事務(wù)。

  比如說,ServiceB.methodB的事務(wù)級(jí)別定義為PROPAGATION_REQUIRED, 那么由于執(zhí)行ServiceA.methodA的時(shí)候,ServiceA.methodA已經(jīng)起了事務(wù),這時(shí)調(diào)用ServiceB.methodB,ServiceB.methodB看到自己已經(jīng)運(yùn)行在ServiceA.methodA的事務(wù)內(nèi)部,就不再起新的事務(wù)。而假如ServiceA.methodA運(yùn)行的時(shí)候發(fā)現(xiàn)自己沒有在事務(wù)中,他就會(huì)為自己分配一個(gè)事務(wù)。這樣,在ServiceA.methodA或者在ServiceB.methodB內(nèi)的任何地方出現(xiàn)異常,事務(wù)都會(huì)被回滾。即使ServiceB.methodB的事務(wù)已經(jīng)被提交,但是ServiceA.methodA在接下來fail要回滾,ServiceB.methodB也要回滾

  2: PROPAGATION_SUPPORTS
  如果當(dāng)前在事務(wù)中,即以事務(wù)的形式運(yùn)行,如果當(dāng)前不再一個(gè)事務(wù)中,那么就以非事務(wù)的形式運(yùn)行

  3: PROPAGATION_MANDATORY
  必須在一個(gè)事務(wù)中運(yùn)行。也就是說,他只能被一個(gè)父事務(wù)調(diào)用。否則,他就要拋出異常

  4: PROPAGATION_REQUIRES_NEW
  這個(gè)就比較繞口了。 比如我們?cè)O(shè)計(jì)ServiceA.methodA的事務(wù)級(jí)別為PROPAGATION_REQUIRED,ServiceB.methodB的事務(wù)級(jí)別為PROPAGATION_REQUIRES_NEW,那么當(dāng)執(zhí)行到ServiceB.methodB的時(shí)候,ServiceA.methodA所在的事務(wù)就會(huì)掛起,ServiceB.methodB會(huì)起一個(gè)新的事務(wù),等待ServiceB.methodB的事務(wù)完成以后,他才繼續(xù)執(zhí)行。他與PROPAGATION_REQUIRED 的事務(wù)區(qū)別在于事務(wù)的回滾程度了。因?yàn)镾erviceB.methodB是新起一個(gè)事務(wù),那么就是存在兩個(gè)不同的事務(wù)。如果ServiceB.methodB已經(jīng)提交,那么ServiceA.methodA失敗回滾,ServiceB.methodB是不會(huì)回滾的。如果ServiceB.methodB失敗回滾,如果他拋出的異常被ServiceA.methodA捕獲,ServiceA.methodA事務(wù)仍然可能提交。

  5: PROPAGATION_NOT_SUPPORTED
  當(dāng)前不支持事務(wù)。比如ServiceA.methodA的事務(wù)級(jí)別是PROPAGATION_REQUIRED ,而ServiceB.methodB的事務(wù)級(jí)別是PROPAGATION_NOT_SUPPORTED ,那么當(dāng)執(zhí)行到ServiceB.methodB時(shí),ServiceA.methodA的事務(wù)掛起,而他以非事務(wù)的狀態(tài)運(yùn)行完,再繼續(xù)ServiceA.methodA的事務(wù)。

  6: PROPAGATION_NEVER
  不能在事務(wù)中運(yùn)行。假設(shè)ServiceA.methodA的事務(wù)級(jí)別是PROPAGATION_REQUIRED, 而ServiceB.methodB的事務(wù)級(jí)別是PROPAGATION_NEVER ,那么ServiceB.methodB就要拋出異常了。

  7: PROPAGATION_NESTED
  理解Nested的關(guān)鍵是savepoint。他與PROPAGATION_REQUIRES_NEW的區(qū)別是,PROPAGATION_REQUIRES_NEW另起一個(gè)事務(wù),將會(huì)與他的父事務(wù)相互獨(dú)立,而Nested的事務(wù)和他的父事務(wù)是相依的,他的提交是要等和他的父事務(wù)一塊提交的。也就是說,如果父事務(wù)最后回滾,他也要回滾的。

  而Nested事務(wù)的好處是他有一個(gè)savepoint。

ServiceA {     //事務(wù)屬性配置為 PROPAGATION_REQUIRED     void methodA() {         try {             //savepoint             ServiceB.methodB(); //PROPAGATION_NESTED 級(jí)別         } catch (SomeException) {             // 執(zhí)行其他業(yè)務(wù), 如 ServiceC.methodC();         }     } }

  也就是說ServiceB.methodB失敗回滾,那么ServiceA.methodA也會(huì)回滾到savepoint點(diǎn)上,ServiceA.methodA可以選擇另外一個(gè)分支,比如ServiceC.methodC,繼續(xù)執(zhí)行,來嘗試完成自己的事務(wù)。但是這個(gè)事務(wù)并沒有在EJB標(biāo)準(zhǔn)中定義。

3、Spring事務(wù)的隔離級(jí)別(Isolation level)

  由隔離級(jí)別從低到高: 

  1. ISOLATION_DEFAULT:這是一個(gè)PlatfromTransactionManager默認(rèn)的隔離級(jí)別,使用數(shù)據(jù)庫默認(rèn)的事務(wù)隔離級(jí)別。
    另外四個(gè)與JDBC的隔離級(jí)別相對(duì)應(yīng)
  2. ISOLATION_READ_UNCOMMITTED:這是事務(wù)最低的隔離級(jí)別,它充許令外一個(gè)事務(wù)可以看到這個(gè)事務(wù)未提交的數(shù)據(jù)。
    這種隔離級(jí)別會(huì)產(chǎn)生臟讀,不可重復(fù)讀和幻像讀。
  3. ISOLATION_READ_COMMITTED: 保證一個(gè)事務(wù)不能讀到另一個(gè)并行事務(wù)已修改但未提交的數(shù)據(jù)。數(shù)據(jù)提交后才能被讀取。

    避免了臟數(shù)據(jù)。該級(jí)別適應(yīng)于大多數(shù)系統(tǒng)。大多數(shù)主流數(shù)據(jù)庫默認(rèn)的級(jí)別。
  4. ISOLATION_REPEATABLE_READ:它除了保證一個(gè)事務(wù)不能讀取另一個(gè)事務(wù)未提交的數(shù)據(jù)外,還保證了避免下面的情況產(chǎn)生(不可重復(fù)讀)。
        避免了臟讀,不可重復(fù)讀。但是可能出現(xiàn)幻像讀。但是也帶來更多的性能損失。
  5. ISOLATION_SERIALIZABLE 事務(wù)被處理為順序執(zhí)行。
        除了防止臟讀,不可重復(fù)讀外,還避免了幻像讀。這是花費(fèi)最高代價(jià)但是最可靠的事務(wù)隔離級(jí)別。

  什么是臟數(shù)據(jù),臟讀,不可重復(fù)讀,幻覺讀?  

  臟讀: 指當(dāng)一個(gè)事務(wù)正在訪問數(shù)據(jù),并且對(duì)數(shù)據(jù)進(jìn)行了修改,而這種修改還沒有提交到數(shù)據(jù)庫中,這時(shí),另外一個(gè)事務(wù)也訪問這個(gè)數(shù)據(jù),然后使用了這個(gè)數(shù)據(jù)。因?yàn)檫@個(gè)數(shù)據(jù)是還沒有提交的數(shù)據(jù),那么另外一個(gè)事務(wù)讀到的這個(gè)數(shù)據(jù)是臟數(shù)據(jù),依據(jù)臟數(shù)據(jù)所做的操作可能是不正確的。
    
  不可重復(fù)讀: 指在一個(gè)事務(wù)內(nèi),多次讀同一數(shù)據(jù)。在這個(gè)事務(wù)還沒有結(jié)束時(shí),另外一個(gè)事務(wù)也訪問該同一數(shù)據(jù)。 那么,在第一個(gè)事務(wù)中的兩次讀數(shù)據(jù)之間,由于第二個(gè)事務(wù)的修改,那么第一個(gè)事務(wù)兩次讀到的數(shù)據(jù)可能是不一樣的。這樣就發(fā)生了在一個(gè)事務(wù)內(nèi)兩次讀到的數(shù)據(jù)是不一樣的,因此稱為是不可重復(fù)讀。
            
  幻覺讀: 指當(dāng)事務(wù)不是獨(dú)立執(zhí)行時(shí)發(fā)生的一種現(xiàn)象,例如第一個(gè)事務(wù)對(duì)一個(gè)表中的數(shù)據(jù)進(jìn)行了修改,這種修改涉及到表中的全部數(shù)據(jù)行。同時(shí),第二個(gè)事務(wù)也修改這個(gè)表中的數(shù)據(jù),這種修改是向表中插入一行新數(shù)據(jù)。那么,以后就會(huì)發(fā)生操作第一個(gè)事務(wù)的用戶發(fā)現(xiàn)表中還有沒有修改的數(shù)據(jù)行,就好象發(fā)生了幻覺一樣。

  不可重復(fù)讀與幻覺讀區(qū)別:幻覺讀與不可重復(fù)讀有點(diǎn)相似,但是不可重復(fù)讀讀取的數(shù)據(jù)不一致是因?yàn)樗〉臄?shù)據(jù)集被改變了。但是phantom reads所要讀的數(shù)據(jù)的不一致卻不是他所要讀的數(shù)據(jù)集改變,而是他的條件數(shù)據(jù)集改變。比如Select account.id where account.name="ppgogo*",第一次讀去了6個(gè)符合條件的id,第二次讀取的時(shí)候,由于事務(wù)b把一個(gè)帳號(hào)的名字由"dd"改成"ppgogo1",結(jié)果取出來了7個(gè)數(shù)據(jù)。

4、一個(gè)完整的常用的配置方便的事務(wù)配置例子

    <!-- 配置dataSource -->     
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property> </bean>
<!-- 配置transactionManager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/> </bean>
<!-- 定義事務(wù)處理規(guī)則 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 事務(wù)傳播屬性、隔離級(jí)別、最優(yōu)化為只讀事務(wù)、事務(wù)超時(shí) -->
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="true" timeout="-1"/>
</tx:attributes> </tx:advice> <aop:config>
<!-- 定義切面 -->
<aop:pointcut id="pointcutId" expression="execution(* com.google.code..*.*Service.*(..))"/>
<!-- 定義織入點(diǎn)規(guī)則 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcutId"/>
</aop:config>