Spring聲明式事務管理源碼解讀
簡介:事務是所有企業應用系統的核心,之前人們使用ejb的時候,容器事務管理(CMT),是slsb最令人稱道的地方,據說很多人使用ejb,使用slsb就是為了cmt,但是spring出現之后,格局就變了,因為程序員又多了一種選擇,就是聲明式事務管理,聲明式事務管理是基于AOP的,及AOP是它的底層特性,本文的目的就是為了和大家探討一下spring的聲明式事務管理,從源代碼來分析它的背后的思想。(
謝謝異常的建議,因為本文原來沒有簡介)
這個是我昨天在解決問題是看源碼得一點體驗,可能說得比較大概,希望大家多多討論,把本貼得質量提高上去,因為spring實現的事務管理這部分我相信還是有點復雜的。一個人未必能想得十分清楚
在spring的聲明式事務管理中,它是如何判定一個及標記一個方法是否應該是處在事務體之中呢。
首先要理解的是spring是如何來標記一個方法是否應該處在事務體之中的。有這樣一個接口TransactionDefinition,其中定義了很多常量,它還有一個子接口TransactionAttribute,其中只有一個方法rollback。
TransactionDefinition中有很多常量定義,它們分別屬于兩種類型,傳播途徑和隔離級別
-
-
-
-
-
- int PROPAGATION_REQUIRED = 0;
/**
* Support a current transaction, create a new one if none exists.
* Analogous to EJB transaction attribute of the same name.
* <p>This is typically the default setting of a transaction definition.
*/
int PROPAGATION_REQUIRED = 0;
當然其中也定義了隔離級別
/**
- * A constant indicating that dirty reads are prevented; non-repeatable reads
- * and phantom reads can occur. This level only prohibits a transaction
- * from reading a row with uncommitted changes in it.
- * @see java.sql.Connection#TRANSACTION_READ_COMMITTED
- */
- int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED;
* A constant indicating that dirty reads are prevented; non-repeatable reads
* and phantom reads can occur. This level only prohibits a transaction
* from reading a row with uncommitted changes in it.
* @see java.sql.Connection#TRANSACTION_READ_COMMITTED
*/
int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED;
同時還有兩個對應的方法來得到這樣的傳播途徑和隔離級別
-
-
-
-
-
-
- int getPropagationBehavior();
-
-
-
-
-
-
-
-
-
-
-
- int getIsolationLevel();
/**
* Return the propagation behavior.
* Must return one of the PROPAGATION constants.
* @see #PROPAGATION_REQUIRED
* @see org.springframework.transaction.support.TransactionSynchronizationManager#isActualTransactionActive()
*/
int getPropagationBehavior();
/**
* Return the isolation level.
* Must return one of the ISOLATION constants.
* <p>Only makes sense in combination with PROPAGATION_REQUIRED or
* PROPAGATION_REQUIRES_NEW.
* <p>Note that a transaction manager that does not support custom
* isolation levels will throw an exception when given any other level
* than ISOLATION_DEFAULT.
* @see #ISOLATION_DEFAULT
*/
int getIsolationLevel();
這個接口有一個默認的實現DefaultTransactionDefinition。然后它還有子類,比如說
DefaultTransactionAttribute。Spring在判斷一個方法是否需要事務體的時候其實是創建一個TransactionAttribute實現的實例.
有了上面的簡單介紹就可以進入真正判斷是否需要事務的地方了。這個方法在TransactionAspectSupport類里,
-
-
-
-
-
-
-
-
- protected TransactionInfo createTransactionIfNecessary(Method method, Class targetClass) {
-
- final TransactionAttribute sourceAttr =
- this.transactionAttributeSource.getTransactionAttribute(method, targetClass);
- TransactionAttribute txAttr = sourceAttr;
-
-
- if (txAttr != null && txAttr.getName() == null) {
- final String name = methodIdentification(method);
- txAttr = new DelegatingTransactionAttribute(sourceAttr) {
- public String getName() {
- return name;
- }
- };
- }
-
- TransactionInfo txInfo = new TransactionInfo(txAttr, method);
-
- if (txAttr != null) {
-
- if (logger.isDebugEnabled()) {
- logger.debug("Getting transaction for " + txInfo.joinpointIdentification());
- }
-
-
- txInfo.newTransactionStatus(this.transactionManager.getTransaction(txAttr));
- }
- else {
-
-
-
- if (logger.isDebugEnabled())
- logger.debug("Don't need to create transaction for [" + methodIdentification(method) +
- "]: this method isn't transactional");
- }
-
-
-
-
- txInfo.bindToThread();
- return txInfo;
- }
/**
* Create a transaction if necessary.
* @param method method about to execute
* @param targetClass class the method is on
* @return a TransactionInfo object, whether or not a transaction was created.
* The hasTransaction() method on TransactionInfo can be used to tell if there
* was a transaction created.
*/
protected TransactionInfo createTransactionIfNecessary(Method method, Class targetClass) {
// If the transaction attribute is null, the method is non-transactional.
final TransactionAttribute sourceAttr =
this.transactionAttributeSource.getTransactionAttribute(method, targetClass);//就是在這里判斷了這個方法的事務屬性
TransactionAttribute txAttr = sourceAttr;
// If no name specified, apply method identification as transaction name.
if (txAttr != null && txAttr.getName() == null) {
final String name = methodIdentification(method);
txAttr = new DelegatingTransactionAttribute(sourceAttr) {
public String getName() {
return name;
}
};
}
TransactionInfo txInfo = new TransactionInfo(txAttr, method);
//TransactionInfo是TransactionAspectSupport的一個內部類,它的主要功能是記錄方法和對應的事務屬性
if (txAttr != null) {
// We need a transaction for this method
if (logger.isDebugEnabled()) {
logger.debug("Getting transaction for " + txInfo.joinpointIdentification());
}
// The transaction manager will flag an error if an incompatible tx already exists
txInfo.newTransactionStatus(this.transactionManager.getTransaction(txAttr));//這個方法要仔細的看
}
else {
// The TransactionInfo.hasTransaction() method will return
// false. We created it only to preserve the integrity of
// the ThreadLocal stack maintained in this class.
if (logger.isDebugEnabled())
logger.debug("Don't need to create transaction for [" + methodIdentification(method) +
"]: this method isn't transactional");
}
// We always bind the TransactionInfo to the thread, even if we didn't create
// a new transaction here. This guarantees that the TransactionInfo stack
// will be managed correctly even if no transaction was created by this aspect.
txInfo.bindToThread();
return txInfo;
}
TransactionInfo是TransactionAspectSupport的一個內部類,它的主要功能是記錄方法和對應的事務屬性,在上面這個方法的最后,這個TransactionInfo對象被保存到當前線程中。
而這個方法會在事務攔截器TransactionInterceptor中被調用,TransactionInterceptor實際上是TransactionAspectSupport的子類,看看其中的invoke方法:
-
-
-
- Class targetClass = (invocation.getThis() != null) ? invocation.getThis().getClass() : null;
-
-
- TransactionInfo txInfo = createTransactionIfNecessary(invocation.getMethod(), targetClass);
-
- Object retVal = null;
- try {
-
-
-
- retVal = invocation.proceed();
- }
- catch (Throwable ex) {
-
- doCloseTransactionAfterThrowing(txInfo, ex);
- throw ex;
- }
- finally {
- doFinally(txInfo);
- }
- doCommitTransactionAfterReturning(txInfo);
- return retVal;
// Work out the target class: may be <code>null</code>.
// The TransactionAttributeSource should be passed the target class
// as well as the method, which may be from an interface
Class targetClass = (invocation.getThis() != null) ? invocation.getThis().getClass() : null;
// Create transaction if necessary.
TransactionInfo txInfo = createTransactionIfNecessary(invocation.getMethod(), targetClass);
Object retVal = null;
try {
// This is an around advice.
// Invoke the next interceptor in the chain.
// This will normally result in a target object being invoked.
retVal = invocation.proceed();
}
catch (Throwable ex) {
// target invocation exception
doCloseTransactionAfterThrowing(txInfo, ex);
throw ex;
}
finally {
doFinally(txInfo);
}
doCommitTransactionAfterReturning(txInfo);//在這里執行方法結束之后需要的操作
return retVal;
這個方法就如同一般的interceptor需要實現的方法一樣。只不過在這個方法里判斷被反射的方法是否需要事務。
接著我們重點再回頭看一下createTransactionIfNecessary方法里的這一句:
txInfo.newTransactionStatus(this.transactionManager.getTransaction(txAttr));
接著我們就應該去看看這個getTransaction方法了,假設我們是使用hibernate3,其他類似??磄etTransaction之前我們來看一下這兩類和一個接口
接口PlatformTransactionManager
抽象類public abstract class AbstractPlatformTransactionManager implements PlatformTransactionManager
類public class HibernateTransactionManager extends AbstractPlatformTransactionManager,很明顯,這里有一個方法模板模式。
那我們看一下AbstractPlatformTransactionManager中得getTransaction方法:
- public final TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
- Object transaction = doGetTransaction();
-
-
- boolean debugEnabled = logger.isDebugEnabled();
- if (debugEnabled) {
- logger.debug("Using transaction object [" + transaction + "]");
- }
-
- if (definition == null) {
-
- definition = new DefaultTransactionDefinition();
- }
-
- if (isExistingTransaction(transaction)) {
-
- return handleExistingTransaction(definition, transaction, debugEnabled);
- }
-
-
- if (definition.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {
- throw new InvalidTimeoutException("Invalid transaction timeout", definition.getTimeout());
- }
-
-
- if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {
- throw new IllegalTransactionStateException(
- "Transaction propagation 'mandatory' but no existing transaction found");
- }
- else if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||
- definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||
- definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {
- if (debugEnabled) {
- logger.debug("Creating new transaction with name [" + definition.getName() + "]");
- }
- doBegin(transaction, definition);
- boolean newSynchronization = (this.transactionSynchronization != SYNCHRONIZATION_NEVER);
- return newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, null);
- }
- else {
-
- boolean newSynchronization = (this.transactionSynchronization == SYNCHRONIZATION_ALWAYS);
- return newTransactionStatus(definition, null, false, newSynchronization, debugEnabled, null);
- }
- }
public final TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
Object transaction = doGetTransaction();//抽象方法,也需要子類實現,這個方法同樣很重要
// Cache debug flag to avoid repeated checks.
boolean debugEnabled = logger.isDebugEnabled();
if (debugEnabled) {
logger.debug("Using transaction object [" + transaction + "]");
}
if (definition == null) {
// Use defaults if no transaction definition given.
definition = new DefaultTransactionDefinition();
}
if (isExistingTransaction(transaction)) {
// Existing transaction found -> check propagation behavior to find out how to behave.
return handleExistingTransaction(definition, transaction, debugEnabled);
}
// Check definition settings for new transaction.
if (definition.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {
throw new InvalidTimeoutException("Invalid transaction timeout", definition.getTimeout());
}
// No existing transaction found -> check propagation behavior to find out how to behave.
if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {
throw new IllegalTransactionStateException(
"Transaction propagation 'mandatory' but no existing transaction found");
}
else if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||
definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||
definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {
if (debugEnabled) {
logger.debug("Creating new transaction with name [" + definition.getName() + "]");
}
doBegin(transaction, definition);
boolean newSynchronization = (this.transactionSynchronization != SYNCHRONIZATION_NEVER);
return newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, null);
}
else {
// Create "empty" transaction: no actual transaction, but potentially synchronization.
boolean newSynchronization = (this.transactionSynchronization == SYNCHRONIZATION_ALWAYS);
return newTransactionStatus(definition, null, false, newSynchronization, debugEnabled, null);
}
}
上面的代碼很多地方都有解釋,所以很好理解,這段代碼的關鍵部分在doBegin(transaction,definition)這里(這是一個抽象方法,子類必須實現這個方法,
具體依賴于抽象,這個是對方法模板模式的一個概括。),前面講到我們假設是使用hibernate,那么就看看HibernateTransactionManager這個類吧,doBegin里的參數1,transaction其實是HibernateTransactionObject的一個實例,這個實例里主要存放的就是sessionholder,sessionholder里存放的就是開始事務的session和transaction對象,如果之前沒有sessionholder存放到線程中,那么這個HibernateTransactionObject的實例的屬性其實是空的,這一點可以在doBegin方法的實現中看出來
- protected void doBegin(Object transaction, TransactionDefinition definition) {
- if (getDataSource() != null && TransactionSynchronizationManager.hasResource(getDataSource())) {
- throw new IllegalTransactionStateException(
- "Pre-bound JDBC Connection found - HibernateTransactionManager does not support " +
- "running within DataSourceTransactionManager if told to manage the DataSource itself. " +
- "It is recommended to use a single HibernateTransactionManager for all transactions " +
- "on a single DataSource, no matter whether Hibernate or JDBC access.");
- }
-
- Session session = null;
-
- try {
- HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
- if (txObject.getSessionHolder() == null) {
- Interceptor entityInterceptor = getEntityInterceptor();
- Session newSession = (entityInterceptor != null ?
- getSessionFactory().openSession(entityInterceptor) : getSessionFactory().openSession());
- if (logger.isDebugEnabled()) {
- logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction");
- }
- txObject.setSessionHolder(new SessionHolder(newSession), true);
protected void doBegin(Object transaction, TransactionDefinition definition) {
if (getDataSource() != null && TransactionSynchronizationManager.hasResource(getDataSource())) {
throw new IllegalTransactionStateException(
"Pre-bound JDBC Connection found - HibernateTransactionManager does not support " +
"running within DataSourceTransactionManager if told to manage the DataSource itself. " +
"It is recommended to use a single HibernateTransactionManager for all transactions " +
"on a single DataSource, no matter whether Hibernate or JDBC access.");
}
Session session = null;
try {
HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
if (txObject.getSessionHolder() == null) {
Interceptor entityInterceptor = getEntityInterceptor();
Session newSession = (entityInterceptor != null ?
getSessionFactory().openSession(entityInterceptor) : getSessionFactory().openSession());
if (logger.isDebugEnabled()) {
logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction");
}
txObject.setSessionHolder(new SessionHolder(newSession), true);
}//我們看到,如果傳進來的transaction中并沒有存放sessionholder,那么就新建一個session,放到新的sessionholder中,再放到HibernateTransactionObject的實例中去,順便說一下,這個變量的名字取得真是差,雖然是Juergen Hoeller寫的,也要批一下,搞得別人會以為是Transaction的實例
- txObject.getSessionHolder().setSynchronizedWithTransaction(true);
- session = txObject.getSessionHolder().getSession();
-
- Connection con = session.connection();
- Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
- txObject.setPreviousIsolationLevel(previousIsolationLevel);
-
- if (definition.isReadOnly() && txObject.isNewSessionHolder()) {
-
- session.setFlushMode(FlushMode.NEVER);
- }
-
- if (!definition.isReadOnly() && !txObject.isNewSessionHolder()) {
-
- FlushMode flushMode = session.getFlushMode();
- if (FlushMode.NEVER.equals(flushMode)) {
- session.setFlushMode(FlushMode.AUTO);
-
- txObject.getSessionHolder().setPreviousFlushMode(flushMode);
- }
- }
-
-
- txObject.getSessionHolder().setTransaction(session.beginTransaction());
-
-
- if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
- txObject.getSessionHolder().setTimeoutInSeconds(definition.getTimeout());
- }
-
-
- if (getDataSource() != null) {
- ConnectionHolder conHolder = new ConnectionHolder(con);
- if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
- conHolder.setTimeoutInSeconds(definition.getTimeout());
- }
- if (logger.isDebugEnabled()) {
- logger.debug("Exposing Hibernate transaction as JDBC transaction [" + con + "]");
- }
- TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
- txObject.setConnectionHolder(conHolder);
- }
-
-
- if (txObject.isNewSessionHolder()) {
- TransactionSynchronizationManager.bindResource(getSessionFactory(), txObject.getSessionHolder());
- }
- }
- catch (Exception ex) {
- SessionFactoryUtils.releaseSession(session, getSessionFactory());
- throw new CannotCreateTransactionException("Could not open Hibernate Session for transaction", ex);
- }
- }
txObject.getSessionHolder().setSynchronizedWithTransaction(true);
session = txObject.getSessionHolder().getSession();
Connection con = session.connection();
Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
txObject.setPreviousIsolationLevel(previousIsolationLevel);
if (definition.isReadOnly() && txObject.isNewSessionHolder()) {
// Just set to NEVER in case of a new Session for this transaction.
session.setFlushMode(FlushMode.NEVER);
}//如果是只讀事務,并且sessionholder是新建的,那么就設置hibernate的flushmode為never
if (!definition.isReadOnly() && !txObject.isNewSessionHolder()) {
// We need AUTO or COMMIT for a non-read-only transaction.
FlushMode flushMode = session.getFlushMode();
if (FlushMode.NEVER.equals(flushMode)) {
session.setFlushMode(FlushMode.AUTO);
//如果session的flushmode是nerver,就設置為auto,因為如果事務定義成非readonly,那么這個session一定是可以flush的
txObject.getSessionHolder().setPreviousFlushMode(flushMode);
}
}
// Add the Hibernate transaction to the session holder.
txObject.getSessionHolder().setTransaction(session.beginTransaction());//開始一個事務,并把這個事務對象放到sessionholder中,隨后這個sessionholder會通過threadlocal放到線程中,以供在commit時使用
// Register transaction timeout.
if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
txObject.getSessionHolder().setTimeoutInSeconds(definition.getTimeout());//設置超時時間,如果其超時時間為-1,則不進行設置,如果不是-1,那么超時時間是這樣設置的new Date(System.currentTimeMillis() + millis*1000);既程序員在配置文件中指定的其實是秒數
}
// Register the Hibernate Session's JDBC Connection for the DataSource, if set.
if (getDataSource() != null) {
ConnectionHolder conHolder = new ConnectionHolder(con);
if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
conHolder.setTimeoutInSeconds(definition.getTimeout());
}
if (logger.isDebugEnabled()) {
logger.debug("Exposing Hibernate transaction as JDBC transaction [" + con + "]");
}
TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
txObject.setConnectionHolder(conHolder);
}
// Bind the session holder to the thread.
if (txObject.isNewSessionHolder()) {
TransactionSynchronizationManager.bindResource(getSessionFactory(), txObject.getSessionHolder());//如果是新的sessionholder則綁定到線程。這樣在進入方法棧中的下一個方法時就能得到整個sessionholder了,connectionholder亦是如此
}
}
catch (Exception ex) {
SessionFactoryUtils.releaseSession(session, getSessionFactory());//如果拋出異常就釋放這個session,這個操作還會在后面出現
throw new CannotCreateTransactionException("Could not open Hibernate Session for transaction", ex);
}
}
通過以上對代碼的注釋可以知道,如果給service設置聲明式事務管理,假設事務傳播途徑為required,然后一個service調用另一個service時,他們其實是共用一個session,原則是沒有就創建,有就不創建,并返回之前已創建的session和transaction。也就是說spring通過threadlocal把session和對應的transaction放到線程之中,保證了在整個方法棧的任何一個地方都能得到同一個session和transaction。
所以如果你的方法在事務體之內,那么你只要通過hibernatesupportdao或者hibernatetemplate來得到session的話,那這個session一定是開始事務的那個session,這個得到session的主要方法在SessionFactoryUtils里,我們來看一下
(這里還有一個小細節,public abstract class SessionFactoryUtils ,Juergen Hoeller在寫工具類的時候為了不能讓其有實例使用的是abstract,而我們一般的做法是final類加private的構造方法,看上去不怎么雅觀,看看源代碼還是能學習到不少寫代碼的技巧的,這里還有一個插曲,上次feiing還說java為什么不能弄成final和abstract同時存在呢,這樣就可以確保既不會有實例產生,也不能繼承了,呵呵)
在SessionFactoryUtils的doGetSession里寫到,如果當前線程有綁定session,則返回這個session,如果沒有綁定session,則看是否允許創建(既allowCreate這個參數是true還是false,這個參數將會在很多地方設計到,比如說hibernatetemplate和hibernatedaosupport里都有),如果不允許創建就拋出一個原始的hibernateException,舉個例子,如果你沒有給某個service方法配置聲明式事務管理,而卻要在這個service所調用的dao里得到當前得session,這樣就會拋這個錯了:
- if (method.getName().equals("getCurrentSession")) {
-
- try {
- return SessionFactoryUtils.doGetSession((SessionFactory) proxy, false);
-
- }
- catch (IllegalStateException ex) {
- throw new HibernateException(ex.getMessage());
- }
- }
if (method.getName().equals("getCurrentSession")) {
// Handle getCurrentSession method: return transactional Session, if any.
try {
return SessionFactoryUtils.doGetSession((SessionFactory) proxy, false);
//最后一個參數是false,說明這個方法不能返回一個新的session,沒有就拋異常
}
catch (IllegalStateException ex) {
throw new HibernateException(ex.getMessage());
}
}
到這里事務開始部分基本就結束了
按正常流程,那么接下來就是方法結束commit的問題了。