oracle事物的传播属性,spring事务的隔离级别和传播属性
/*** @author 王政* @date 2006-11-24* @note 轉載自http://www.iteye.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;*************************************************************************在這篇文章里,他用兩個嵌套的例子輔助分析,我這里直接引用了。********************sample***********************ServiceA {/*** 事務屬性配置為 PROPAGATION_REQUIRED*/void methodA() {ServiceB.methodB();}}ServiceB {/*** 事務屬性配置為 PROPAGATION_REQUIRED*/void methodB() {}}????? *************************************************我們這里一個個分析吧1: PROPAGATION_REQUIRED加入當前正要執行的事務不在另外一個事務里,那么就起一個新的事務比如說,ServiceB.methodB的事務級別定義為PROPAGATION_REQUIRED, 那么由于執行ServiceA.methodA的時候,ServiceA.methodA已經起了事務,這時調用ServiceB.methodB,ServiceB.methodB看到自己已經運行在ServiceA.methodA的事務內部,就不再起新的事務。而假如ServiceA.methodA運行的時候發現自己沒有在事務中,他就會為自己分配一個事務。這樣,在ServiceA.methodA或者在ServiceB.methodB內的任何地方出現異常,事務都會被回滾。即使ServiceB.methodB的事務已經被提交,但是ServiceA.methodA在接下來fail要回滾,ServiceB.methodB也要回滾2:?? PROPAGATION_SUPPORTS如果當前在事務中,即以事務的形式運行,如果當前不再一個事務中,那么就以非事務的形式運行這就跟平常用的普通非事務的代碼只有一點點區別了。不理這個,因為我也沒有覺得有什么區別3:?? PROPAGATION_MANDATORY必須在一個事務中運行。也就是說,他只能被一個父事務調用。否則,他就要拋出異常。4:?? PROPAGATION_REQUIRES_NEW這個就比較繞口了。 比如我們設計ServiceA.methodA的事務級別為PROPAGATION_REQUIRED,ServiceB.methodB的事務級別為PROPAGATION_REQUIRES_NEW,那么當執行到ServiceB.methodB的時候,ServiceA.methodA所在的事務就會掛起,ServiceB.methodB會起一個新的事務,等待ServiceB.methodB的事務完成以后,他才繼續執行。他與PROPAGATION_REQUIRED 的事務區別在于事務的回滾程度了。因為ServiceB.methodB是新起一個事務,那么就是存在兩個不同的事務。如果ServiceB.methodB已經提交,那么ServiceA.methodA失敗回滾,ServiceB.methodB是不會回滾的。如果ServiceB.methodB失敗回滾,如果他拋出的異常被ServiceA.methodA捕獲,ServiceA.methodA事務仍然可能提交。5:?? PROPAGATION_NOT_SUPPORTED當前不支持事務。比如ServiceA.methodA的事務級別是PROPAGATION_REQUIRED ,而ServiceB.methodB的事務級別是PROPAGATION_NOT_SUPPORTED ,那么當執行到ServiceB.methodB時,ServiceA.methodA的事務掛起,而他以非事務的狀態運行完,再繼續ServiceA.methodA的事務。6:?? PROPAGATION_NEVER不能在事務中運行。假設ServiceA.methodA的事務級別是PROPAGATION_REQUIRED, 而ServiceB.methodB的事務級別是PROPAGATION_NEVER ,那么ServiceB.methodB就要拋出異常了。7:?? PROPAGATION_NESTED理解Nested的關鍵是savepoint。他與PROPAGATION_REQUIRES_NEW的區別是,PROPAGATION_REQUIRES_NEW另起一個事務,將會與他的父事務相互獨立,而Nested的事務和他的父事務是相依的,他的提交是要等和他的父事務一塊提交的。也就是說,如果父事務最后回滾,他也要回滾的。而Nested事務的好處是他有一個savepoint。*****************************************ServiceA {/*** 事務屬性配置為 PROPAGATION_REQUIRED*/void methodA() {try {//savepointServiceB.methodB();??? //PROPAGATION_NESTED 級別} catch (SomeException) {// 執行其他業務, 如 ServiceC.methodC();}}}********************************************也就是說ServiceB.methodB失敗回滾,那么ServiceA.methodA也會回滾到savepoint點上,ServiceA.methodA可以選擇另外一個分支,比如ServiceC.methodC,繼續執行,來嘗試完成自己的事務。但是這個事務并沒有在EJB標準中定義。二、Isolation Level(事務隔離等級):
總結
以上是生活随笔為你收集整理的oracle事物的传播属性,spring事务的隔离级别和传播属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle中的open,Oracle
- 下一篇: 鸡柳是什么?