1 初步理解
理解事務之前,先講一個你日常生活中最常干的事:取錢。
比如你去ATM機取1000塊錢,大體有兩個步驟:首先輸入密碼金額,銀行卡扣掉1000元錢;然后ATM出1000元錢。這兩個步驟必須是要么都執(zhí)行要么都不執(zhí)行。如果銀行卡扣除了1000塊但是ATM出錢失敗的話,你將會損失1000元;如果銀行卡扣錢失敗但是ATM卻出了1000塊,那么銀行將損失1000元。所以,如果一個步驟成功另一個步驟失敗對雙方都不是好事,如果不管哪一個步驟失敗了以后,整個取錢過程都能回滾,也就是完全取消所有操作的話,這對雙方都是極好的。
事務就是用來解決類似問題的。事務是一系列的動作,它們綜合在一起才是一個完整的工作單元,這些動作必須全部完成,如果有一個失敗的話,那么事務就會回滾到最開始的狀態(tài),仿佛什么都沒發(fā)生過一樣。
在企業(yè)級應用程序開發(fā)中,事務管理必不可少的技術,用來確保數(shù)據(jù)的完整性和一致性。
事務有四個特性:ACID
原子性(Atomicity):事務是一個原子操作,由一系列動作組成。事務的原子性確保動作要么全部完成,要么完全不起作用。
一致性(Consistency):一旦事務完成(不管成功還是失?。?,系統(tǒng)必須確保它所建模的業(yè)務處于一致的狀態(tài),而不會是部分完成部分失敗。在現(xiàn)實中的數(shù)據(jù)不應該被破壞。
隔離性(Isolation):可能有許多事務會同時處理相同的數(shù)據(jù),因此每個事務都應該與其他事務隔離開來,防止數(shù)據(jù)損壞。
持久性(Durability):一旦事務完成,無論發(fā)生什么系統(tǒng)錯誤,它的結果都不應該受到影響,這樣就能從任何系統(tǒng)崩潰中恢復過來。通常情況下,事務的結果被寫到持久化存儲器中。
2 核心接口
Spring事務管理的實現(xiàn)有許多細節(jié),如果對整個接口框架有個大體了解會非常有利于我們理解事務,下面通過講解Spring的事務接口來了解Spring實現(xiàn)事務的具體策略。
Spring事務管理涉及的接口的聯(lián)系如下:
2.1 事務管理器
Spring并不直接管理事務,而是提供了多種事務管理器,他們將事務管理的職責委托給Hibernate或者JTA等持久化機制所提供的相關平臺框架的事務來實現(xiàn)。
Spring事務管理器的接口是org.springframework.transaction.PlatformTransactionManager,通過這個接口,Spring為各個平臺如JDBC、Hibernate等都提供了對應的事務管理器,但是具體的實現(xiàn)就是各個平臺自己的事情了。此接口的內(nèi)容如下:
Public interface PlatformTransactionManager()...{? ??
? // 由TransactionDefinition得到TransactionStatus對象? ?
?TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;??
? // 提交? ? Void commit(TransactionStatus status) throws TransactionException;? ? ?
?// 回滾? ??
Void rollback(TransactionStatus status) throws TransactionException;? ??
? }
從這里可知具體的具體的事務管理機制對Spring來說是透明的,它并不關心那些,那些是對應各個平臺需要關心的,所以Spring事務管理的一個優(yōu)點就是為不同的事務API提供一致的編程模型,如JTA、JDBC、Hibernate、JPA。下面分別介紹各個平臺框架實現(xiàn)事務管理的機制。
2.1.1 JDBC事務
如果應用程序中直接使用JDBC來進行持久化,DataSourceTransactionManager會為你處理事務邊界。為了使用DataSourceTransactionManager,你需要使用如下的XML將其裝配到應用程序的上下文定義中:
<propertyname="dataSource"ref="dataSource"/>
實際上,DataSourceTransactionManager是通過調(diào)用java.sql.Connection來管理事務,而后者是通過DataSource獲取到的。通過調(diào)用連接的commit()方法來提交事務,同樣,事務失敗則通過調(diào)用rollback()方法進行回滾。
2.1.2 Hibernate事務
如果應用程序的持久化是通過Hibernate實習的,那么你需要使用HibernateTransactionManager。對于Hibernate3,需要在Spring上下文定義中添加如下的<bean>聲明:
<propertyname="sessionFactory"ref="sessionFactory"/>
sessionFactory屬性需要裝配一個Hibernate的session工廠,HibernateTransactionManager的實現(xiàn)細節(jié)是它將事務管理的職責委托給org.hibernate.Transaction對象,而后者是從Hibernate Session中獲取到的。當事務成功完成時,HibernateTransactionManager將會調(diào)用Transaction對象的commit()方法,反之,將會調(diào)用rollback()方法。
2.1.3 Java持久化API事務(JPA)
Hibernate多年來一直是事實上的Java持久化標準,但是現(xiàn)在Java持久化API作為真正的Java持久化標準進入大家的視野。如果你計劃使用JPA的話,那你需要使用Spring的JpaTransactionManager來處理事務。你需要在Spring中這樣配置JpaTransactionManager:
<propertyname="sessionFactory"ref="sessionFactory"/>
JpaTransactionManager只需要裝配一個JPA實體管理工廠(javax.persistence.EntityManagerFactory接口的任意實現(xiàn))。JpaTransactionManager將與由工廠所產(chǎn)生的JPA EntityManager合作來構建事務。
2.1.4 Java原生API事務
如果你沒有使用以上所述的事務管理,或者是跨越了多個事務管理源(比如兩個或者是多個不同的數(shù)據(jù)源),你就需要使用JtaTransactionManager:
<propertyname="transactionManagerName"value="java:/TransactionManager"/>
JtaTransactionManager將事務管理的責任委托給javax.transaction.UserTransaction和javax.transaction.TransactionManager對象,其中事務成功完成通過UserTransaction.commit()方法提交,事務失敗通過UserTransaction.rollback()方法回滾。
2.2 基本事務屬性的定義
上面講到的事務管理器接口PlatformTransactionManager通過getTransaction(TransactionDefinition definition)方法來得到事務,這個方法里面的參數(shù)是TransactionDefinition類,這個類就定義了一些基本的事務屬性。
那么什么是事務屬性呢?事務屬性可以理解成事務的一些基本配置,描述了事務策略如何應用到方法上。事務屬性包含了5個方面,如圖所示:
而TransactionDefinition接口內(nèi)容如下:
publicinterfaceTransactionDefinition{intgetPropagationBehavior();// 返回事務的傳播行為intgetIsolationLevel();// 返回事務的隔離級別,事務管理器根據(jù)它來控制另外一個事務可以看到本事務內(nèi)的哪些數(shù)據(jù)intgetTimeout();// 返回事務必須在多少秒內(nèi)完成booleanisReadOnly();// 事務是否只讀,事務管理器能夠根據(jù)這個返回值進行優(yōu)化,確保事務是只讀的}
我們可以發(fā)現(xiàn)TransactionDefinition正好用來定義事務屬性,下面詳細介紹一下各個事務屬性。
2.2.1 傳播行為
事務的第一個方面是傳播行為(propagation behavior)。當事務方法被另一個事務方法調(diào)用時,必須指定事務應該如何傳播。例如:方法可能繼續(xù)在現(xiàn)有事務中運行,也可能開啟一個新事務,并在自己的事務中運行。Spring定義了七種傳播行為:
傳播行為含義
PROPAGATION_REQUIRED表示當前方法必須運行在事務中。如果當前事務存在,方法將會在該事務中運行。否則,會啟動一個新的事務
PROPAGATION_SUPPORTS表示當前方法不需要事務上下文,但是如果存在當前事務的話,那么該方法會在這個事務中運行
PROPAGATION_MANDATORY表示該方法必須在事務中運行,如果當前事務不存在,則會拋出一個異常
PROPAGATION_REQUIRED_NEW表示當前方法必須運行在它自己的事務中。一個新的事務將被啟動。如果存在當前事務,在該方法執(zhí)行期間,當前事務會被掛起。如果使用JTATransactionManager的話,則需要訪問TransactionManager
PROPAGATION_NOT_SUPPORTED表示該方法不應該運行在事務中。如果存在當前事務,在該方法運行期間,當前事務將被掛起。如果使用JTATransactionManager的話,則需要訪問TransactionManager
PROPAGATION_NEVER表示當前方法不應該運行在事務上下文中。如果當前正有一個事務在運行,則會拋出異常
PROPAGATION_NESTED表示如果當前已經(jīng)存在一個事務,那么該方法將會在嵌套事務中運行。嵌套的事務可以獨立于當前事務進行單獨地提交或回滾。如果當前事務不存在,那么其行為與PROPAGATION_REQUIRED一樣。注意各廠商對這種傳播行為的支持是有所差異的??梢詤⒖假Y源管理器的文檔來確認它們是否支持嵌套事務
注:以下具體講解傳播行為的內(nèi)容參考自Spring事務機制詳解
(1)PROPAGATION_REQUIRED 如果存在一個事務,則支持當前事務。如果沒有事務則開啟一個新的事務。
//事務屬性 PROPAGATION_REQUIREDmethodA{? ? ……methodB();
? ? ……
}
//事務屬性 PROPAGATION_REQUIREDmethodB{? ……}
使用spring聲明式事務,spring使用AOP來支持聲明式事務,會根據(jù)事務屬性,自動在方法調(diào)用之前決定是否開啟一個事務,并在方法執(zhí)行之后決定事務提交或回滾事務。
單獨調(diào)用methodB方法:
main{metodB(); }
相當于
Main{? ?
?Connection con=null;
try{? ??
?con = getConnection();? ? ? ??
?con.setAutoCommit(false);
//方法調(diào)用
methodB();
//提交事務con.commit();? ??
?}
Catch(RuntimeException ex) {
//回滾事務
con.rollback();? ?
?? }finally{
//釋放資源
closeCon();? ?
?}?
}
Spring保證在methodB方法中所有的調(diào)用都獲得到一個相同的連接。在調(diào)用methodB時,沒有一個存在的事務,所以獲得一個新的連接,開啟了一個新的事務。
單獨調(diào)用MethodA時,在MethodA內(nèi)又會調(diào)用MethodB.
執(zhí)行效果相當于:
main{? ??
?Connection con =null;
try{? ? ? ?
?con = getConnection();? ? ??
? methodA();? ? ?
?? con.commit();??
? }catch(RuntimeException ex) {? ??
? ? con.rollback();? ?
?}finally{? ? ??
? ? ? closeCon();?
?? }?
?}
調(diào)用MethodA時,環(huán)境中沒有事務,所以開啟一個新的事務.當在MethodA中調(diào)用MethodB時,環(huán)境中已經(jīng)有了一個事務,所以methodB就加入當前事務。
(2)PROPAGATION_SUPPORTS 如果存在一個事務,支持當前事務。如果沒有事務,則非事務的執(zhí)行。但是對于事務同步的事務管理器,PROPAGATION_SUPPORTS與不使用事務有少許不同。
//事務屬性 PROPAGATION_REQUIREDmethodA(){methodB();}//事務屬性 PROPAGATION_SUPPORTSmethodB(){
}
單純的調(diào)用methodB時,methodB方法是非事務的執(zhí)行的。當調(diào)用methdA時,methodB則加入了methodA的事務中,事務地執(zhí)行。
(3)PROPAGATION_MANDATORY 如果已經(jīng)存在一個事務,支持當前事務。如果沒有一個活動的事務,則拋出異常。
//事務屬性 PROPAGATION_REQUIREDmethodA(){methodB();}//事務屬性 PROPAGATION_MANDATORYmethodB(){
? ? ……
}
當單獨調(diào)用methodB時,因為當前沒有一個活動的事務,則會拋出異常throw new IllegalTransactionStateException(“Transaction propagation ‘mandatory’ but no existing transaction found”);當調(diào)用methodA時,methodB則加入到methodA的事務中,事務地執(zhí)行。
(4)PROPAGATION_REQUIRES_NEW 總是開啟一個新的事務。如果一個事務已經(jīng)存在,則將這個存在的事務掛起。
//事務屬性 PROPAGATION_REQUIREDmethodA(){doSomeThingA();methodB();doSomeThingB();}//事務屬性 PROPAGATION_REQUIRES_NEWmethodB(){
? ? ……
}
調(diào)用A方法:
main(){methodA();
}
相當于
main(){?
?? TransactionManager tm =null;
try{
//獲得一個JTA事務管理器
tm = getTransactionManager();? ? ? ??
tm.begin();
//開啟一個新的事務
Transaction ts1 = tm.getTransaction();? ??
? doSomeThing();? ? ? ?
?tm.suspend();
//掛起當前事務
try{
//重新開啟第二個事務? ? ? ? ?
?? tm.begin();
Transaction ts2 = tm.getTransaction();? ? ? ? ?
? methodB();? ? ? ? ?
//提交第二個事務
?? ts2.commit();
} Catch(RunTimeException ex) {? ? ? ? ? ?
//回滾第二個事務
?ts2.rollback();
}finally{
//釋放資源
}
//methodB執(zhí)行完后,恢復第一個事務
tm.resume(ts1);? ? ? ?
?doSomeThingB();? ??
//提交第一個事務??
? ts1.commit();
} catch(RunTimeException ex) {? ? ? ??
//回滾第一個事務
ts1.rollback();}
finally{//釋放資源}}
在這里,我把ts1稱為外層事務,ts2稱為內(nèi)層事務。從上面的代碼可以看出,ts2與ts1是兩個獨立的事務,互不相干。Ts2是否成功并不依賴于 ts1。如果methodA方法在調(diào)用methodB方法后的doSomeThingB方法失敗了,而methodB方法所做的結果依然被提交。而除了 methodB之外的其它代碼導致的結果卻被回滾了。使用PROPAGATION_REQUIRES_NEW,需要使用 JtaTransactionManager作為事務管理器。
(5)PROPAGATION_NOT_SUPPORTED 總是非事務地執(zhí)行,并掛起任何存在的事務。使用PROPAGATION_NOT_SUPPORTED,也需要使用JtaTransactionManager作為事務管理器。(代碼示例同上,可同理推出)
(6)PROPAGATION_NEVER 總是非事務地執(zhí)行,如果存在一個活動事務,則拋出異常。
(7)PROPAGATION_NESTED如果一個活動的事務存在,則運行在一個嵌套的事務中. 如果沒有活動事務, 則按TransactionDefinition.PROPAGATION_REQUIRED 屬性執(zhí)行。這是一個嵌套事務,使用JDBC 3.0驅(qū)動時,僅僅支持DataSourceTransactionManager作為事務管理器。需要JDBC 驅(qū)動的java.sql.Savepoint類。有一些JTA的事務管理器實現(xiàn)可能也提供了同樣的功能。使用PROPAGATION_NESTED,還需要把PlatformTransactionManager的nestedTransactionAllowed屬性設為true;而 nestedTransactionAllowed屬性值默認為false。
//事務屬性 PROPAGATION_REQUIREDmethodA(){doSomeThingA();methodB();doSomeThingB();}//事務屬性 PROPAGATION_NESTEDmethodB(){
? ? ……
}
如果單獨調(diào)用methodB方法,則按REQUIRED屬性執(zhí)行。如果調(diào)用methodA方法,相當于下面的效果:
main(){? ??
Connection con =null;? ?
?Savepoint savepoint =null;
try{? ? ? ??
con = getConnection();? ? ? ??
con.setAutoCommit(false);? ? ?
?? doSomeThingA();? ? ? ?
?savepoint = con2.setSavepoint();
try{? ? ? ? ? ??
methodB();? ?
?? ? }catch(RuntimeException ex) {? ? ? ??
? ? con.rollback(savepoint);? ? ?
?? }finally{//釋放資源}? ? ?
?? doSomeThingB();? ? ??
? con.commit();? ? }
catch(RuntimeException ex) {? ??
? ? con.rollback();? ? }
finally{//釋放資源}}
當methodB方法調(diào)用之前,調(diào)用setSavepoint方法,保存當前的狀態(tài)到savepoint。如果methodB方法調(diào)用失敗,則恢復到之前保存的狀態(tài)。但是需要注意的是,這時的事務并沒有進行提交,如果后續(xù)的代碼(doSomeThingB()方法)調(diào)用失敗,則回滾包括methodB方法的所有操作。
嵌套事務一個非常重要的概念就是內(nèi)層事務依賴于外層事務。外層事務失敗時,會回滾內(nèi)層事務所做的動作。而內(nèi)層事務操作失敗并不會引起外層事務的回滾。
PROPAGATION_NESTED 與PROPAGATION_REQUIRES_NEW的區(qū)別:它們非常類似,都像一個嵌套事務,如果不存在一個活動的事務,都會開啟一個新的事務。使用 PROPAGATION_REQUIRES_NEW時,內(nèi)層事務與外層事務就像兩個獨立的事務一樣,一旦內(nèi)層事務進行了提交后,外層事務不能對其進行回滾。兩個事務互不影響。兩個事務不是一個真正的嵌套事務。同時它需要JTA事務管理器的支持。
使用PROPAGATION_NESTED時,外層事務的回滾可以引起內(nèi)層事務的回滾。而內(nèi)層事務的異常并不會導致外層事務的回滾,它是一個真正的嵌套事務。DataSourceTransactionManager使用savepoint支持PROPAGATION_NESTED時,需要JDBC 3.0以上驅(qū)動及1.4以上的JDK版本支持。其它的JTA TrasactionManager實現(xiàn)可能有不同的支持方式。
PROPAGATION_REQUIRES_NEW 啟動一個新的, 不依賴于環(huán)境的 “內(nèi)部” 事務. 這個事務將被完全 commited 或 rolled back 而不依賴于外部事務, 它擁有自己的隔離范圍, 自己的鎖, 等等. 當內(nèi)部事務開始執(zhí)行時, 外部事務將被掛起, 內(nèi)務事務結束時, 外部事務將繼續(xù)執(zhí)行。
另一方面, PROPAGATION_NESTED 開始一個 “嵌套的” 事務, 它是已經(jīng)存在事務的一個真正的子事務. 潛套事務開始執(zhí)行時, 它將取得一個 savepoint. 如果這個嵌套事務失敗, 我們將回滾到此 savepoint. 潛套事務是外部事務的一部分, 只有外部事務結束后它才會被提交。
由此可見, PROPAGATION_REQUIRES_NEW 和 PROPAGATION_NESTED 的最大區(qū)別在于, PROPAGATION_REQUIRES_NEW 完全是一個新的事務, 而 PROPAGATION_NESTED 則是外部事務的子事務, 如果外部事務 commit, 嵌套事務也會被 commit, 這個規(guī)則同樣適用于 roll back.
PROPAGATION_REQUIRED應該是我們首先的事務傳播行為。它能夠滿足我們大多數(shù)的事務需求。
事務的第二個維度就是隔離級別(isolation level)。隔離級別定義了一個事務可能受其他并發(fā)事務影響的程度。
(1)并發(fā)事務引起的問題
在典型的應用程序中,多個事務并發(fā)運行,經(jīng)常會操作相同的數(shù)據(jù)來完成各自的任務。并發(fā)雖然是必須的,但可能會導致一下的問題。
臟讀(Dirty reads)——臟讀發(fā)生在一個事務讀取了另一個事務改寫但尚未提交的數(shù)據(jù)時。如果改寫在稍后被回滾了,那么第一個事務獲取的數(shù)據(jù)就是無效的。
不可重復讀(Nonrepeatable read)——不可重復讀發(fā)生在一個事務執(zhí)行相同的查詢兩次或兩次以上,但是每次都得到不同的數(shù)據(jù)時。這通常是因為另一個并發(fā)事務在兩次查詢期間進行了更新。
幻讀(Phantom read)——幻讀與不可重復讀類似。它發(fā)生在一個事務(T1)讀取了幾行數(shù)據(jù),接著另一個并發(fā)事務(T2)插入了一些數(shù)據(jù)時。在隨后的查詢中,第一個事務(T1)就會發(fā)現(xiàn)多了一些原本不存在的記錄。
不可重復讀與幻讀的區(qū)別
不可重復讀的重點是修改:
同樣的條件, 你讀取過的數(shù)據(jù), 再次讀取出來發(fā)現(xiàn)值不一樣了
例如:在事務1中,Mary 讀取了自己的工資為1000,操作并沒有完成
con1 = getConnection();selectsalaryfromemployee empId ="Mary";
在事務2中,這時財務人員修改了Mary的工資為2000,并提交了事務.
con2 = getConnection();updateemployeesetsalary =2000;? ? ? con2.commit();?
在事務1中,Mary 再次讀取自己的工資時,工資變?yōu)榱?000
//con1? selectsalaryfromemployee empId ="Mary";
在一個事務中前后兩次讀取的結果并不一致,導致了不可重復讀。
幻讀的重點在于新增或者刪除:
同樣的條件, 第1次和第2次讀出來的記錄數(shù)不一樣
例如:目前工資為1000的員工有10人。事務1,讀取所有工資為1000的員工。
con1 = getConnection();Select*fromemployeewheresalary =1000;
共讀取10條記錄
這時另一個事務向employee表插入了一條員工記錄,工資也為1000
con2 = getConnection();Insertintoemployee(empId,salary)values("Lili",1000);? ? ? con2.commit();?
事務1再次讀取所有工資為1000的員工
//con1? select*fromemployeewheresalary =1000;
共讀取到了11條記錄,這就產(chǎn)生了幻像讀。
從總的結果來看, 似乎不可重復讀和幻讀都表現(xiàn)為兩次讀取的結果不一致。但如果你從控制的角度來看, 兩者的區(qū)別就比較大。
對于前者, 只需要鎖住滿足條件的記錄。
對于后者, 要鎖住滿足條件及其相近的記錄。
(2)隔離級別
隔離級別含義
ISOLATION_DEFAULT使用后端數(shù)據(jù)庫默認的隔離級別
ISOLATION_READ_UNCOMMITTED最低的隔離級別,允許讀取尚未提交的數(shù)據(jù)變更,可能會導致臟讀、幻讀或不可重復讀
ISOLATION_READ_COMMITTED允許讀取并發(fā)事務已經(jīng)提交的數(shù)據(jù),可以阻止臟讀,但是幻讀或不可重復讀仍有可能發(fā)生
ISOLATION_REPEATABLE_READ對同一字段的多次讀取結果都是一致的,除非數(shù)據(jù)是被本身事務自己所修改,可以阻止臟讀和不可重復讀,但幻讀仍有可能發(fā)生
ISOLATION_SERIALIZABLE最高的隔離級別,完全服從ACID的隔離級別,確保阻止臟讀、不可重復讀以及幻讀,也是最慢的事務隔離級別,因為它通常是通過完全鎖定事務相關的數(shù)據(jù)庫表來實現(xiàn)的
事務的第三個特性是它是否為只讀事務。如果事務只對后端的數(shù)據(jù)庫進行該操作,數(shù)據(jù)庫可以利用事務的只讀特性來進行一些特定的優(yōu)化。通過將事務設置為只讀,你就可以給數(shù)據(jù)庫一個機會,讓它應用它認為合適的優(yōu)化措施。
為了使應用程序很好地運行,事務不能運行太長的時間。因為事務可能涉及對后端數(shù)據(jù)庫的鎖定,所以長時間的事務會不必要的占用數(shù)據(jù)庫資源。事務超時就是事務的一個定時器,在特定時間內(nèi)事務如果沒有執(zhí)行完畢,那么就會自動回滾,而不是一直等待其結束。
事務五邊形的最后一個方面是一組規(guī)則,這些規(guī)則定義了哪些異常會導致事務回滾而哪些不會。默認情況下,事務只有遇到運行期異常時才會回滾,而在遇到檢查型異常時不會回滾(這一行為與EJB的回滾行為是一致的)
但是你可以聲明事務在遇到特定的檢查型異常時像遇到運行期異常那樣回滾。同樣,你還可以聲明事務遇到特定的異常不回滾,即使這些異常是運行期異常。
上面講到的調(diào)用PlatformTransactionManager接口的getTransaction()的方法得到的是TransactionStatus接口的一個實現(xiàn),這個接口的內(nèi)容如下:
publicinterfaceTransactionStatus{booleanisNewTransaction();// 是否是新的事物booleanhasSavepoint();// 是否有恢復點voidsetRollbackOnly();// 設置為只回滾booleanisRollbackOnly();// 是否為只回滾booleanisCompleted;// 是否已完成}
可以發(fā)現(xiàn)這個接口描述的是一些處理事務提供簡單的控制事務執(zhí)行和查詢事務狀態(tài)的方法,在回滾或提交的時候需要應用對應的事務狀態(tài)。
Spring提供了對編程式事務和聲明式事務的支持,編程式事務允許用戶在代碼中精確定義事務的邊界,而聲明式事務(基于AOP)有助于用戶將操作與事務規(guī)則進行解耦。
簡單地說,編程式事務侵入到了業(yè)務代碼里面,但是提供了更加詳細的事務管理;而聲明式事務由于基于AOP,所以既能起到事務管理的作用,又可以不影響業(yè)務代碼的具體實現(xiàn)。
Spring提供兩種方式的編程式事務管理,分別是:使用TransactionTemplate和直接使用PlatformTransactionManager。
采用TransactionTemplate和采用其他Spring模板,如JdbcTempalte和HibernateTemplate是一樣的方法。它使用回調(diào)方法,把應用程序從處理取得和釋放資源中解脫出來。如同其他模板,TransactionTemplate是線程安全的。代碼片段:
TransactionTemplate tt =newTransactionTemplate();// 新建一個TransactionTemplateObject result = tt.execute(newTransactionCallback(){publicObjectdoTransaction(TransactionStatus status){? ? ? ? ? ? ? ? ? updateOperation();returnresultOfUpdateOperation();? ? ? ? ? ? ? }? ? ? });// 執(zhí)行execute方法進行事務管理
使用TransactionCallback()可以返回一個值。如果使用TransactionCallbackWithoutResult則沒有返回值。
3.2.2 使用PlatformTransactionManager
示例代碼如下:
DataSourceTransactionManager dataSourceTransactionManager =newDataSourceTransactionManager();//定義一個某個框架平臺的TransactionManager,如JDBC、HibernatedataSourceTransactionManager.setDataSource(this.getJdbcTemplate().getDataSource());// 設置數(shù)據(jù)源DefaultTransactionDefinition transDef =newDefaultTransactionDefinition();// 定義事務屬性transDef.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED);// 設置傳播行為屬性TransactionStatus status = dataSourceTransactionManager.getTransaction(transDef);// 獲得事務狀態(tài)try{// 數(shù)據(jù)庫操作dataSourceTransactionManager.commit(status);// 提交}catch(Exception e) {? ? ? ? dataSourceTransactionManager.rollback(status);// 回滾}
注:以下配置代碼參考自Spring事務配置的五種方式
根據(jù)代理機制的不同,總結了五種Spring事務的配置方式,配置文件如下:
(1)每個Bean都有一個代理
<?xml version="1.0"encoding="UTF-8"?><beansxmlns="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"><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="configLocation"value="classpath:hibernate.cfg.xml"/><propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration"/></bean><!-- 定義事務管理器(聲明式的事務) --><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><!-- 配置DAO --><beanid="userDaoTarget"class="com.bluesky.spring.dao.UserDaoImpl"><propertyname="sessionFactory"ref="sessionFactory"/></bean><beanid="userDao"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><!-- 配置事務管理器 --><propertyname="transactionManager"ref="transactionManager"/><propertyname="target"ref="userDaoTarget"/><propertyname="proxyInterfaces"value="com.bluesky.spring.dao.GeneratorDao"/><!-- 配置事務屬性 --><propertyname="transactionAttributes"><props><propkey="*">PROPAGATION_REQUIRED</prop></props></property></bean></beans>
(2)所有Bean共享一個代理基類
<?xml version="1.0"encoding="UTF-8"?><beansxmlns="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"><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="configLocation"value="classpath:hibernate.cfg.xml"/><propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration"/></bean><!-- 定義事務管理器(聲明式的事務) --><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><beanid="transactionBase"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"lazy-init="true"abstract="true"><!-- 配置事務管理器 --><propertyname="transactionManager"ref="transactionManager"/><!-- 配置事務屬性 --><propertyname="transactionAttributes"><props><propkey="*">PROPAGATION_REQUIRED</prop></props></property></bean><!-- 配置DAO --><beanid="userDaoTarget"class="com.bluesky.spring.dao.UserDaoImpl"><propertyname="sessionFactory"ref="sessionFactory"/></bean><beanid="userDao"parent="transactionBase"><propertyname="target"ref="userDaoTarget"/></bean></beans>
(3)使用攔截器
<?xml version="1.0"encoding="UTF-8"?><beansxmlns="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"><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="configLocation"value="classpath:hibernate.cfg.xml"/><propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration"/></bean><!-- 定義事務管理器(聲明式的事務) --><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><beanid="transactionInterceptor"class="org.springframework.transaction.interceptor.TransactionInterceptor"><propertyname="transactionManager"ref="transactionManager"/><!-- 配置事務屬性 --><propertyname="transactionAttributes"><props><propkey="*">PROPAGATION_REQUIRED</prop></props></property></bean><beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><propertyname="beanNames"><list><value>*Dao</value></list></property><propertyname="interceptorNames"><list><value>transactionInterceptor</value></list></property></bean><!-- 配置DAO --><beanid="userDao"class="com.bluesky.spring.dao.UserDaoImpl"><propertyname="sessionFactory"ref="sessionFactory"/></bean></beans>
(4)使用tx標簽配置的攔截器
<?xml version="1.0"encoding="UTF-8"?><beansxmlns="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-scanbase-package="com.bluesky"/><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="configLocation"value="classpath:hibernate.cfg.xml"/><propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration"/></bean><!-- 定義事務管理器(聲明式的事務) --><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><tx:adviceid="txAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="*"propagation="REQUIRED"/></tx:attributes></tx:advice><aop:config><aop:pointcutid="interceptorPointCuts"expression="execution(* com.bluesky.spring.dao.*.*(..))"/><aop:advisoradvice-ref="txAdvice"pointcut-ref="interceptorPointCuts"/></aop:config></beans>
(5)全注解
<?xml version="1.0"encoding="UTF-8"?><beansxmlns="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-scanbase-package="com.bluesky"/><tx:annotation-driventransaction-manager="transactionManager"/><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="configLocation"value="classpath:hibernate.cfg.xml"/><propertyname="configurationClass"value="org.hibernate.cfg.AnnotationConfiguration"/></bean><!-- 定義事務管理器(聲明式的事務) --><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean></beans>
此時在DAO上需加上@Transactional注解,如下:
packagecom.bluesky.spring.dao;importjava.util.List;importorg.hibernate.SessionFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;importorg.springframework.stereotype.Component;importcom.bluesky.spring.domain.User;@Transactional@Component("userDao")publicclassUserDaoImplextendsHibernateDaoSupportimplementsUserDao{publicListlistUsers(){returnthis.getSession().createQuery("from User").list();? ? }? }
注:該實例參考自Spring中的事務管理實例詳解
首先是數(shù)據(jù)庫表
book(isbn, book_name, price)
account(username, balance)
book_stock(isbn, stock)
然后是XML配置
<beansxmlns="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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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"><importresource="applicationContext-db.xml"/><context:component-scanbase-package="com.springinaction.transaction"></context:component-scan><tx:annotation-driventransaction-manager="txManager"/><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean></beans>
使用的類
BookShopDao
package com.springinaction.transaction;publicinterfaceBookShopDao{// 根據(jù)書號獲取書的單價publicintfindBookPriceByIsbn(String isbn);// 更新書的庫存,使書號對應的庫存-1publicvoidupdateBookStock(String isbn);// 更新用戶的賬戶余額:account的balance-pricepublicvoidupdateUserAccount(String username, intprice);}
BookShopDaoImpl
packagecom.springinaction.transaction;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.stereotype.Repository;@Repository("bookShopDao")publicclassBookShopDaoImplimplementsBookShopDao{@AutowiredprivateJdbcTemplate JdbcTemplate;@OverridepublicintfindBookPriceByIsbn(String isbn){? ? ? ? String sql ="SELECT price FROM book WHERE isbn = ?";returnJdbcTemplate.queryForObject(sql, Integer.class, isbn);? ? }@OverridepublicvoidupdateBookStock(String isbn){//檢查書的庫存是否足夠,若不夠,則拋出異常String sql2 ="SELECT stock FROM book_stock WHERE isbn = ?";intstock = JdbcTemplate.queryForObject(sql2, Integer.class, isbn);if(stock ==0) {thrownewBookStockException("庫存不足!");? ? ? ? }? ? ? ? String sql ="UPDATE book_stock SET stock = stock - 1 WHERE isbn = ?";? ? ? ? JdbcTemplate.update(sql, isbn);? ? }@OverridepublicvoidupdateUserAccount(String username, intprice){//檢查余額是否不足,若不足,則拋出異常String sql2 ="SELECT balance FROM account WHERE username = ?";intbalance = JdbcTemplate.queryForObject(sql2, Integer.class, username);if(balance < price) {thrownewUserAccountException("余額不足!");? ? ? ? }? ? ? ? ? ? ? String sql ="UPDATE account SET balance = balance - ? WHERE username = ?";? ? ? ? JdbcTemplate.update(sql, price, username);? ? }}
BookShopService
package com.springinaction.transaction;publicinterfaceBookShopService{publicvoidpurchase(String username, String isbn);}
BookShopServiceImpl
packagecom.springinaction.transaction;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Isolation;importorg.springframework.transaction.annotation.Propagation;importorg.springframework.transaction.annotation.Transactional;@Service("bookShopService")publicclassBookShopServiceImplimplementsBookShopService{@AutowiredprivateBookShopDao bookShopDao;/**
? ? * 1.添加事務注解
? ? * 使用propagation 指定事務的傳播行為,即當前的事務方法被另外一個事務方法調(diào)用時如何使用事務。
? ? * 默認取值為REQUIRED,即使用調(diào)用方法的事務
? ? * REQUIRES_NEW:使用自己的事務,調(diào)用的事務方法的事務被掛起。
? ? *
? ? * 2.使用isolation 指定事務的隔離級別,最常用的取值為READ_COMMITTED
? ? * 3.默認情況下 Spring 的聲明式事務對所有的運行時異常進行回滾,也可以通過對應的屬性進行設置。通常情況下,默認值即可。
? ? * 4.使用readOnly 指定事務是否為只讀。 表示這個事務只讀取數(shù)據(jù)但不更新數(shù)據(jù),這樣可以幫助數(shù)據(jù)庫引擎優(yōu)化事務。若真的是一個只讀取數(shù)據(jù)庫值得方法,應設置readOnly=true
? ? * 5.使用timeOut 指定強制回滾之前事務可以占用的時間。
? ? */@Transactional(propagation=Propagation.REQUIRES_NEW,? ? ? ? ? ? isolation=Isolation.READ_COMMITTED,? ? ? ? ? ? noRollbackFor={UserAccountException.class},? ? ? ? ? ? readOnly=true, timeout=3)@Overridepublicvoidpurchase(String username, String isbn){//1.獲取書的單價intprice = bookShopDao.findBookPriceByIsbn(isbn);//2.更新書的庫存bookShopDao.updateBookStock(isbn);//3.更新用戶余額bookShopDao.updateUserAccount(username, price);? ? }}
Cashier
packagecom.springinaction.transaction;importjava.util.List;publicinterfaceCashier{publicvoidcheckout(String username, List<String>isbns);}
CashierImpl:CashierImpl.checkout和bookShopService.purchase聯(lián)合測試了事務的傳播行為
packagecom.springinaction.transaction;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;@Service("cashier")publicclassCashierImplimplementsCashier{@AutowiredprivateBookShopService bookShopService;@Transactional@Overridepublicvoidcheckout(String username, List<String> isbns){for(String isbn : isbns) {? ? ? ? ? ? bookShopService.purchase(username, isbn);? ? ? ? }? ? }}
BookStockException
packagecom.springinaction.transaction;publicclassBookStockExceptionextendsRuntimeException{privatestaticfinallongserialVersionUID =1L;publicBookStockException(){super();// TODO Auto-generated constructor stub}publicBookStockException(String arg0, Throwable arg1, booleanarg2,booleanarg3){super(arg0, arg1, arg2, arg3);// TODO Auto-generated constructor stub}publicBookStockException(String arg0, Throwable arg1){super(arg0, arg1);// TODO Auto-generated constructor stub}publicBookStockException(String arg0){super(arg0);// TODO Auto-generated constructor stub}publicBookStockException(Throwable arg0){super(arg0);// TODO Auto-generated constructor stub}}
UserAccountException
packagecom.springinaction.transaction;publicclassUserAccountExceptionextendsRuntimeException{privatestaticfinallongserialVersionUID =1L;publicUserAccountException(){super();// TODO Auto-generated constructor stub}publicUserAccountException(String arg0, Throwable arg1, booleanarg2,booleanarg3){super(arg0, arg1, arg2, arg3);// TODO Auto-generated constructor stub}publicUserAccountException(String arg0, Throwable arg1){super(arg0, arg1);// TODO Auto-generated constructor stub}publicUserAccountException(String arg0){super(arg0);// TODO Auto-generated constructor stub}publicUserAccountException(Throwable arg0){super(arg0);// TODO Auto-generated constructor stub}}
測試類
packagecom.springinaction.transaction;importjava.util.Arrays;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassSpringTransitionTest{privateApplicationContext ctx =null;privateBookShopDao bookShopDao =null;privateBookShopService bookShopService =null;privateCashier cashier =null;? ? {? ? ? ? ctx =newClassPathXmlApplicationContext("config/transaction.xml");? ? ? ? bookShopDao = ctx.getBean(BookShopDao.class);? ? ? ? bookShopService = ctx.getBean(BookShopService.class);? ? ? ? cashier = ctx.getBean(Cashier.class);? ? }@TestpublicvoidtestBookShopDaoFindPriceByIsbn(){? ? ? ? System.out.println(bookShopDao.findBookPriceByIsbn("1001"));? ? }@TestpublicvoidtestBookShopDaoUpdateBookStock(){? ? ? ? bookShopDao.updateBookStock("1001");? ? }@TestpublicvoidtestBookShopDaoUpdateUserAccount(){? ? ? ? bookShopDao.updateUserAccount("AA",100);? ? }@TestpublicvoidtestBookShopService(){? ? ? ? bookShopService.purchase("AA","1001");? ? }@TestpublicvoidtestTransactionPropagation(){? ? ? ? cashier.checkout("AA", Arrays.asList("1001","1002"));? ? }}
本文章轉(zhuǎn)載自spring事務管理(詳解和實例)