javascript
Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别、不可重复读与幻读的区别
前些天發(fā)現(xiàn)了一個巨牛的人工智能學(xué)習(xí)網(wǎng)站,通俗易懂,風(fēng)趣幽默,忍不住分享一下給大家。點擊跳轉(zhuǎn)到教程。
spring事務(wù)配置的五種方式
?
前段時間對Spring的事務(wù)配置做了比較深入的研究,在此之間對Spring的事務(wù)配置雖說也配置過,但是一直沒有一個清楚的認識。通過這次的學(xué)習(xí)發(fā)覺Spring的事務(wù)配置只要把思路理清,還是比較好掌握的。??? 總結(jié)如下:
??? Spring配置文件中關(guān)于事務(wù)配置總是由三個組成部分,分別是DataSource、TransactionManager和代理機制這三部分,無論哪種配置方式,一般變化的只是代理機制這部分。
??? DataSource、TransactionManager這兩部分只是會根據(jù)數(shù)據(jù)訪問方式有所變化,比如使用hibernate進行數(shù)據(jù)訪問時,DataSource實際為SessionFactory,TransactionManager的實現(xiàn)為HibernateTransactionManager。
? ?
?
??? 根據(jù)代理機制的不同,總結(jié)了五種Spring事務(wù)的配置方式,配置文件如下:
??? 第一種方式:每個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>??
????<!--?定義事務(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共享一個代理基類
<?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>
????
????<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>
</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>??
????<!--?定義事務(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>
</beans>
第四種方式:使用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>??????
</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>??
????<!--?定義事務(wù)管理器(聲明式的事務(wù))?-->??
????<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里面事務(wù)的傳播屬性和事務(wù)隔離級別
?
?
| ? 一、Propagation (事務(wù)的傳播屬性) Propagation : key屬性確定代理應(yīng)該給哪個方法增加事務(wù)行為。這樣的屬性最重要的部份是傳播行為。有以下選項可供使用:?? PROPAGATION_REQUIRED--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就新建一個事務(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ù)不在另外一個事務(wù)里,那么就起一個新的事務(wù) 比如說,ServiceB.methodB的事務(wù)級別定義為PROPAGATION_REQUIRED, 那么由于執(zhí)行ServiceA.methodA的時候, ServiceA.methodA已經(jīng)起了事務(wù),這時調(diào)用ServiceB.methodB,ServiceB.methodB看到自己已經(jīng)運行在ServiceA.methodA 的事務(wù)內(nèi)部,就不再起新的事務(wù)。而假如ServiceA.methodA運行的時候發(fā)現(xiàn)自己沒有在事務(wù)中,他就會為自己分配一個事務(wù)。 這樣,在ServiceA.methodA或者在ServiceB.methodB內(nèi)的任何地方出現(xiàn)異常,事務(wù)都會被回滾。即使ServiceB.methodB的事務(wù)已經(jīng)被 提交,但是ServiceA.methodA在接下來fail要回滾,ServiceB.methodB也要回滾 2: PROPAGATION_SUPPORTS 如果當(dāng)前在事務(wù)中,即以事務(wù)的形式運行,如果當(dāng)前不再一個事務(wù)中,那么就以非事務(wù)的形式運行 3: PROPAGATION_MANDATORY 必須在一個事務(wù)中運行。也就是說,他只能被一個父事務(wù)調(diào)用。否則,他就要拋出異常 4: PROPAGATION_REQUIRES_NEW 這個就比較繞口了。 比如我們設(shè)計ServiceA.methodA的事務(wù)級別為PROPAGATION_REQUIRED,ServiceB.methodB的事務(wù)級別為PROPAGATION_REQUIRES_NEW, 那么當(dāng)執(zhí)行到ServiceB.methodB的時候,ServiceA.methodA所在的事務(wù)就會掛起,ServiceB.methodB會起一個新的事務(wù),等待ServiceB.methodB的事務(wù)完成以后, 他才繼續(xù)執(zhí)行。他與PROPAGATION_REQUIRED 的事務(wù)區(qū)別在于事務(wù)的回滾程度了。因為ServiceB.methodB是新起一個事務(wù),那么就是存在 兩個不同的事務(wù)。如果ServiceB.methodB已經(jīng)提交,那么ServiceA.methodA失敗回滾,ServiceB.methodB是不會回滾的。如果ServiceB.methodB失敗回滾, 如果他拋出的異常被ServiceA.methodA捕獲,ServiceA.methodA事務(wù)仍然可能提交。 5: PROPAGATION_NOT_SUPPORTED 當(dāng)前不支持事務(wù)。比如ServiceA.methodA的事務(wù)級別是PROPAGATION_REQUIRED ,而ServiceB.methodB的事務(wù)級別是PROPAGATION_NOT_SUPPORTED , 那么當(dāng)執(zhí)行到ServiceB.methodB時,ServiceA.methodA的事務(wù)掛起,而他以非事務(wù)的狀態(tài)運行完,再繼續(xù)ServiceA.methodA的事務(wù)。 6: PROPAGATION_NEVER 不能在事務(wù)中運行。假設(shè)ServiceA.methodA的事務(wù)級別是PROPAGATION_REQUIRED, 而ServiceB.methodB的事務(wù)級別是PROPAGATION_NEVER , 那么ServiceB.methodB就要拋出異常了。 7: PROPAGATION_NESTED 理解Nested的關(guān)鍵是savepoint。他與PROPAGATION_REQUIRES_NEW的區(qū)別是,PROPAGATION_REQUIRES_NEW另起一個事務(wù),將會與他的父事務(wù)相互獨立, 而Nested的事務(wù)和他的父事務(wù)是相依的,他的提交是要等和他的父事務(wù)一塊提交的。也就是說,如果父事務(wù)最后回滾,他也要回滾的。 而Nested事務(wù)的好處是他有一個savepoint。 ***************************************** ServiceA { /** * 事務(wù)屬性配置為 PROPAGATION_REQUIRED */ void methodA() { try { //savepoint ServiceB.methodB(); //PROPAGATION_NESTED 級別 } catch (SomeException) { // 執(zhí)行其他業(yè)務(wù), 如 ServiceC.methodC(); } } } ******************************************** 也就是說ServiceB.methodB失敗回滾,那么ServiceA.methodA也會回滾到savepoint點上,ServiceA.methodA可以選擇另外一個分支,比如 ServiceC.methodC,繼續(xù)執(zhí)行,來嘗試完成自己的事務(wù)。 但是這個事務(wù)并沒有在EJB標(biāo)準(zhǔn)中定義。 二、Isolation Level(事務(wù)隔離等級): 1、Serializable:最嚴(yán)格的級別,事務(wù)串行執(zhí)行,資源消耗最大; 2、REPEATABLE READ:保證了一個事務(wù)不會修改已經(jīng)由另一個事務(wù)讀取但未提交(回滾)的數(shù)據(jù)。避免了“臟讀取”和“不可重復(fù)讀取”的情況,但是帶來了更多的性能損失。 3、READ COMMITTED:大多數(shù)主流數(shù)據(jù)庫的默認事務(wù)等級,保證了一個事務(wù)不會讀到另一個并行事務(wù)已修改但未提交的數(shù)據(jù),避免了“臟讀取”。該級別適用于大多數(shù)系統(tǒng)。 4、Read Uncommitted:保證了讀取過程中不會讀取到非法數(shù)據(jù)。隔離級別在于處理多事務(wù)的并發(fā)問題。 我們知道并行可以提高數(shù)據(jù)庫的吞吐量和效率,但是并不是所有的并發(fā)事務(wù)都可以并發(fā)運行,這需要查看數(shù)據(jù)庫教材的可串行化條件判斷了。 這里就不闡述。 我們首先說并發(fā)中可能發(fā)生的3中不討人喜歡的事情 1: Dirty reads--讀臟數(shù)據(jù)。也就是說,比如事務(wù)A的未提交(還依然緩存)的數(shù)據(jù)被事務(wù)B讀走,如果事務(wù)A失敗回滾,會導(dǎo)致事務(wù)B所讀取的的數(shù)據(jù)是錯誤的。 2: non-repeatable reads--數(shù)據(jù)不可重復(fù)讀。比如事務(wù)A中兩處讀取數(shù)據(jù)-total-的值。在第一讀的時候,total是100,然后事務(wù)B就把total的數(shù)據(jù)改成 200,事務(wù)A再讀一次,結(jié)果就發(fā)現(xiàn),total竟然就變成200了,造成事務(wù)A數(shù)據(jù)混亂。 3: phantom reads--幻象讀數(shù)據(jù),這個和non-repeatable reads相似,也是同一個事務(wù)中多次讀不一致的問題。但是non-repeatable reads的不一致是因為他所要取的數(shù)據(jù)集被改變了(比如total的數(shù)據(jù)),但是phantom reads所要讀的數(shù)據(jù)的不一致卻不是他所要讀的數(shù)據(jù)集改變,而是他的條件數(shù)據(jù)集改變。比如Select account.id where account.name="ppgogo*",第一次讀去了6個符合條件的id,第二次讀取的時候,由于事務(wù)b把一個帳號的名字由"dd"改成"ppgogo1",結(jié)果取出來了7個數(shù)據(jù)。 ? ?
? ? 三、readOnly 事務(wù)屬性中的readOnly標(biāo)志表示對應(yīng)的事務(wù)應(yīng)該被最優(yōu)化為只讀事務(wù)。 這是一個最優(yōu)化提示。在一些情況下,一些事務(wù)策略能夠起到顯著的最優(yōu)化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)時避免dirty checking(試圖“刷新”)。 四、Timeout 在事務(wù)屬性中還有定義“timeout”值的選項,指定事務(wù)超時為幾秒。在JTA中,這將被簡單地傳遞到J2EE服務(wù)器的事務(wù)協(xié)調(diào)程序,并據(jù)此得到相應(yīng)的解釋 ? |
spring里面事務(wù)的傳播屬性和事務(wù)隔離級別
?
| ? 一、Propagation (事務(wù)的傳播屬性) Propagation : key屬性確定代理應(yīng)該給哪個方法增加事務(wù)行為。這樣的屬性最重要的部份是傳播行為。有以下選項可供使用:PROPAGATION_REQUIRED--支持當(dāng)前事務(wù),如果當(dāng)前沒有事務(wù),就新建一個事務(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ù)不在另外一個事務(wù)里,那么就起一個新的事務(wù) 比如說,ServiceB.methodB的事務(wù)級別定義為PROPAGATION_REQUIRED, 那么由于執(zhí)行ServiceA.methodA的時候, ServiceA.methodA已經(jīng)起了事務(wù),這時調(diào)用ServiceB.methodB,ServiceB.methodB看到自己已經(jīng)運行在ServiceA.methodA 的事務(wù)內(nèi)部,就不再起新的事務(wù)。而假如ServiceA.methodA運行的時候發(fā)現(xiàn)自己沒有在事務(wù)中,他就會為自己分配一個事務(wù)。 這樣,在ServiceA.methodA或者在ServiceB.methodB內(nèi)的任何地方出現(xiàn)異常,事務(wù)都會被回滾。即使ServiceB.methodB的事務(wù)已經(jīng)被 提交,但是ServiceA.methodA在接下來fail要回滾,ServiceB.methodB也要回滾 2: PROPAGATION_SUPPORTS 如果當(dāng)前在事務(wù)中,即以事務(wù)的形式運行,如果當(dāng)前不再一個事務(wù)中,那么就以非事務(wù)的形式運行 3: PROPAGATION_MANDATORY 必須在一個事務(wù)中運行。也就是說,他只能被一個父事務(wù)調(diào)用。否則,他就要拋出異常 4: PROPAGATION_REQUIRES_NEW 這個就比較繞口了。 比如我們設(shè)計ServiceA.methodA的事務(wù)級別為PROPAGATION_REQUIRED,ServiceB.methodB的事務(wù)級別為PROPAGATION_REQUIRES_NEW, 那么當(dāng)執(zhí)行到ServiceB.methodB的時候,ServiceA.methodA所在的事務(wù)就會掛起,ServiceB.methodB會起一個新的事務(wù),等待ServiceB.methodB的事務(wù)完成以后, 他才繼續(xù)執(zhí)行。他與PROPAGATION_REQUIRED 的事務(wù)區(qū)別在于事務(wù)的回滾程度了。因為ServiceB.methodB是新起一個事務(wù),那么就是存在 兩個不同的事務(wù)。如果ServiceB.methodB已經(jīng)提交,那么ServiceA.methodA失敗回滾,ServiceB.methodB是不會回滾的。如果ServiceB.methodB失敗回滾, 如果他拋出的異常被ServiceA.methodA捕獲,ServiceA.methodA事務(wù)仍然可能提交。 5: PROPAGATION_NOT_SUPPORTED 當(dāng)前不支持事務(wù)。比如ServiceA.methodA的事務(wù)級別是PROPAGATION_REQUIRED ,而ServiceB.methodB的事務(wù)級別是PROPAGATION_NOT_SUPPORTED , 那么當(dāng)執(zhí)行到ServiceB.methodB時,ServiceA.methodA的事務(wù)掛起,而他以非事務(wù)的狀態(tài)運行完,再繼續(xù)ServiceA.methodA的事務(wù)。 6: PROPAGATION_NEVER 不能在事務(wù)中運行。假設(shè)ServiceA.methodA的事務(wù)級別是PROPAGATION_REQUIRED, 而ServiceB.methodB的事務(wù)級別是PROPAGATION_NEVER , 那么ServiceB.methodB就要拋出異常了。 7: PROPAGATION_NESTED 理解Nested的關(guān)鍵是savepoint。他與PROPAGATION_REQUIRES_NEW的區(qū)別是,PROPAGATION_REQUIRES_NEW另起一個事務(wù),將會與他的父事務(wù)相互獨立, 而Nested的事務(wù)和他的父事務(wù)是相依的,他的提交是要等和他的父事務(wù)一塊提交的。也就是說,如果父事務(wù)最后回滾,他也要回滾的。 而Nested事務(wù)的好處是他有一個savepoint。 ***************************************** ServiceA { /** * 事務(wù)屬性配置為 PROPAGATION_REQUIRED */ void methodA() { try { //savepoint ServiceB.methodB(); //PROPAGATION_NESTED 級別 } catch (SomeException) { // 執(zhí)行其他業(yè)務(wù), 如 ServiceC.methodC(); } } } ******************************************** 也就是說ServiceB.methodB失敗回滾,那么ServiceA.methodA也會回滾到savepoint點上,ServiceA.methodA可以選擇另外一個分支,比如 ServiceC.methodC,繼續(xù)執(zhí)行,來嘗試完成自己的事務(wù)。 但是這個事務(wù)并沒有在EJB標(biāo)準(zhǔn)中定義。 二、Isolation Level(事務(wù)隔離等級): 1、Serializable:最嚴(yán)格的級別,事務(wù)串行執(zhí)行,資源消耗最大; 2、REPEATABLE READ:保證了一個事務(wù)不會修改已經(jīng)由另一個事務(wù)讀取但未提交(回滾)的數(shù)據(jù)。避免了“臟讀取”和“不可重復(fù)讀取”的情況,但是帶來了更多的性能損失。 3、READ COMMITTED:大多數(shù)主流數(shù)據(jù)庫的默認事務(wù)等級,保證了一個事務(wù)不會讀到另一個并行事務(wù)已修改但未提交的數(shù)據(jù),避免了“臟讀取”。該級別適用于大多數(shù)系統(tǒng)。 4、Read Uncommitted:保證了讀取過程中不會讀取到非法數(shù)據(jù)。隔離級別在于處理多事務(wù)的并發(fā)問題。 我們知道并行可以提高數(shù)據(jù)庫的吞吐量和效率,但是并不是所有的并發(fā)事務(wù)都可以并發(fā)運行,這需要查看數(shù)據(jù)庫教材的可串行化條件判斷了。 這里就不闡述。 我們首先說并發(fā)中可能發(fā)生的3中不討人喜歡的事情 1: Dirty reads--讀臟數(shù)據(jù)。也就是說,比如事務(wù)A的未提交(還依然緩存)的數(shù)據(jù)被事務(wù)B讀走,如果事務(wù)A失敗回滾,會導(dǎo)致事務(wù)B所讀取的的數(shù)據(jù)是錯誤的。 2: non-repeatable reads--數(shù)據(jù)不可重復(fù)讀。比如事務(wù)A中兩處讀取數(shù)據(jù)-total-的值。在第一讀的時候,total是100,然后事務(wù)B就把total的數(shù)據(jù)改成 200,事務(wù)A再讀一次,結(jié)果就發(fā)現(xiàn),total竟然就變成200了,造成事務(wù)A數(shù)據(jù)混亂。 3: phantom reads--幻象讀數(shù)據(jù),這個和non-repeatable reads相似,也是同一個事務(wù)中多次讀不一致的問題。但是non-repeatable reads的不一致是因為他所要取的數(shù)據(jù)集被改變了(比如total的數(shù)據(jù)),但是phantom reads所要讀的數(shù)據(jù)的不一致卻不是他所要讀的數(shù)據(jù)集改變,而是他的條件數(shù)據(jù)集改變。比如Select account.id where account.name="ppgogo*",第一次讀去了6個符合條件的id,第二次讀取的時候,由于事務(wù)b把一個帳號的名字由"dd"改成"ppgogo1",結(jié)果取出來了7個數(shù)據(jù)。 ? ?
? ? 三、readOnly 事務(wù)屬性中的readOnly標(biāo)志表示對應(yīng)的事務(wù)應(yīng)該被最優(yōu)化為只讀事務(wù)。 這是一個最優(yōu)化提示。在一些情況下,一些事務(wù)策略能夠起到顯著的最優(yōu)化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)時避免dirty checking(試圖“刷新”)。 四、Timeout 在事務(wù)屬性中還有定義“timeout”值的選項,指定事務(wù)超時為幾秒。在JTA中,這將被簡單地傳遞到J2EE服務(wù)器的事務(wù)協(xié)調(diào)程序,并據(jù)此得到相應(yīng)的解釋 ? ? 20110112 數(shù)據(jù)庫提供了四種事務(wù)隔離級別, 不同的隔離級別采用不同的鎖類開來實現(xiàn).? |
?
總結(jié)
以上是生活随笔為你收集整理的Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别、不可重复读与幻读的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在ASP.NET中利JavaScript
- 下一篇: Spring 定时任务的几种实现