Posted on 2010-04-28 14:30
瘋狂 閱讀(396)
評(píng)論(0) 編輯 收藏 所屬分類:
spring 、
hibernate
轉(zhuǎn)載自:http://blog.chinaunix.net/u1/55983/showart_2091761.html
7個(gè)傳播行為,4個(gè)隔離級(jí)別,
Spring事務(wù)的傳播行為和隔離級(jí)別[transaction behavior and isolated level]2007-08-01 16:33事務(wù)的傳播行為和隔離級(jí)別[transaction behavior and isolated level]
Spring中事務(wù)的定義:
一、Propagation :
key屬性確定代理應(yīng)該給哪個(gè)方法增加事務(wù)行為。這樣的屬性最重要的部份是傳播行為。有以下選項(xiàng)可供使用:
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ù),則拋出異常。
很多人看到事務(wù)的傳播行為屬性都不甚了解,我昨晚看了j2ee without ejb的時(shí)候,看到這里也不了解,甚至重新翻起數(shù)據(jù)庫(kù)系統(tǒng)的教材書,但是也沒有找到對(duì)這個(gè)的分析。今天搜索,找到一篇極好的分析文章,雖然這篇文章是重點(diǎn)分析PROPAGATION_REQUIRED 和 PROPAGATION_REQUIRED_NESTED的
====================================================
解惑 spring 嵌套事務(wù)
/**
* @date 2006-11-24
* @note 轉(zhuǎn)載自http://www.javaeye.com/topic/35907?page=1
*/
********TransactionDefinition 接口定義*******************
/**
* Support a current transaction, create a new one if none exists.
* Analogous to EJB transaction attribute of the same name.
*
This is typically the default setting of a transaction definition.
*/
int PROPAGATION_REQUIRED = 0;
/**
* Support a current transaction, execute non-transactionally if none exists.
* Analogous to EJB transaction attribute of the same name.
*
Note: For transaction managers with transaction synchronization,
* PROPAGATION_SUPPORTS is slightly different from no transaction at all,
* as it defines a transaction scopp that synchronization will apply for.
* As a consequence, the same resources (JDBC Connection, Hibernate Session, etc)
* will be shared for the entire specified scope. Note that this depends on
* the actual synchronization configuration of the transaction manager.
* @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization
*/
int PROPAGATION_SUPPORTS = 1;
/**
* Support a current transaction, throw an exception if none exists.
* Analogous to EJB transaction attribute of the same name.
*/
int PROPAGATION_MANDATORY = 2;
/**
* Create a new transaction, suspend the current transaction if one exists.
* Analogous to EJB transaction attribute of the same name.
*
Note: Actual transaction suspension will not work on out-of-the-box
* on all transaction managers. This in particular applies to JtaTransactionManager,
* which requires the javax.transaction.TransactionManager to be
* made available it to it (which is server-specific in standard J2EE).
* @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
*/
int PROPAGATION_REQUIRES_NEW = 3;
/**
* Execute non-transactionally, suspend the current transaction if one exists.
* Analogous to EJB transaction attribute of the same name.
*
Note: Actual transaction suspension will not work on out-of-the-box
* on all transaction managers. This in particular applies to JtaTransactionManager,
* which requires the javax.transaction.TransactionManager to be
* made available it to it (which is server-specific in standard J2EE).
* @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
*/
int PROPAGATION_NOT_SUPPORTED = 4;
/**
* Execute non-transactionally, throw an exception if a transaction exists.
* Analogous to EJB transaction attribute of the same name.
*/
int PROPAGATION_NEVER = 5;
/**
* Execute within a nested transaction if a current transaction exists,
* behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.
*
Note: Actual creation of a nested transaction will only work on specific
* transaction managers. Out of the box, this only applies to the JDBC
* DataSourceTransactionManager when working on a JDBC 3.0 driver.
* Some JTA providers might support nested transactions as well.
* @see org.springframework.jdbc.datasource.DataSourceTransactionManager
*/
int PROPAGATION_NESTED = 6;
*************************************************************************
在這篇文章里,他用兩個(gè)嵌套的例子輔助分析,我這里直接引用了。
********************sample***********************
ServiceA {
/**
* 事務(wù)屬性配置為 PROPAGATION_REQUIRED
*/
void methodA() {
ServiceB.methodB();
}
}
ServiceB {
/**
* 事務(wù)屬性配置為 PROPAGATION_REQUIRED
*/
void methodB() {
}
}
*************************************************
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)中定義。
二、Isolation Level(事務(wù)隔離等級(jí)):
1、Serializable:最嚴(yán)格的級(jí)別,事務(wù)串行執(zhí)行,資源消耗最大;
2、REPEATABLE READ:保證了一個(gè)事務(wù)不會(huì)修改已經(jīng)由另一個(gè)事務(wù)讀取但未提交(回滾)的數(shù)據(jù)。避免了“臟讀取”和“不可重復(fù)讀取”的情況,但是帶來了更多的性能損失。
3、READ COMMITTED:大多數(shù)主流數(shù)據(jù)庫(kù)的默認(rèn)事務(wù)等級(jí),保證了一個(gè)事務(wù)不會(huì)讀到另一個(gè)并行事務(wù)已修改但未提交的數(shù)據(jù),避免了“臟讀取”。該級(jí)別適用于大多數(shù)系統(tǒng)。
4、Read Uncommitted:保證了讀取過程中不會(huì)讀取到非法數(shù)據(jù)。隔離級(jí)別在于處理多事務(wù)的并發(fā)問題。
我們知道并行可以提高數(shù)據(jù)庫(kù)的吞吐量和效率,但是并不是所有的并發(fā)事務(wù)都可以并發(fā)運(yùn)行,這需要查看數(shù)據(jù)庫(kù)教材的可串行化條件判斷了。
這里就不闡述。
我們首先說并發(fā)中可能發(fā)生的3中不討人喜歡的事情
1: Dirty reads--讀臟數(shù)據(jù)。也就是說,比如事務(wù)A的未提交(還依然緩存)的數(shù)據(jù)被事務(wù)B讀走,如果事務(wù)A失敗回滾,會(huì)導(dǎo)致事務(wù)B所讀取的的數(shù)據(jù)是錯(cuò)誤的。
2: non-repeatable reads--數(shù)據(jù)不可重復(fù)讀。比如事務(wù)A中兩處讀取數(shù)據(jù)-total-的值。在第一讀的時(shí)候,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ù),這個(gè)和non-repeatable reads相似,也是同一個(gè)事務(wù)中多次讀不一致的問題。但是non-repeatable reads的不一致是因?yàn)樗〉臄?shù)據(jù)集被改變了(比如total的數(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ù)。 Dirty reads non-repeatable reads phantom reads
Serializable 不會(huì) 不會(huì) 不會(huì)
REPEATABLE READ 不會(huì) 不會(huì) 會(huì)
READ COMMITTED 不會(huì) 會(huì) 會(huì)
Read Uncommitted 會(huì) 會(huì) 會(huì)
三、readOnly
事務(wù)屬性中的readOnly標(biāo)志表示對(duì)應(yīng)的事務(wù)應(yīng)該被最優(yōu)化為只讀事務(wù)。
這是一個(gè)最優(yōu)化提示。在一些情況下,一些事務(wù)策略能夠起到顯著的最優(yōu)化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)時(shí)避免dirty checking(試圖“刷新”)。
四、Timeout
在事務(wù)屬性中還有定義“timeout”值的選項(xiàng),指定事務(wù)超時(shí)為幾秒。在JTA中,這將被簡(jiǎn)單地傳遞到J2EE服務(wù)器的事務(wù)協(xié)調(diào)程序,并據(jù)此得到相應(yīng)的解釋。