xhchc

危波帆墙,笑谈只在桃花上;与谁共尚,风吹万里浪; 相依相偎,不做黄泉想;莫惆怅,碧波潮生,一萧自狂放……

 

Spring声明式事务管理源码解读之事务开始(转)

Spring声明式事务管理源码解读

简介:事务是所有企业应用系统的核心,之前人们使用ejb的时候,容器事务管理(CMT),是slsb最令人称道的地方,据说很多人使用ejb,使用slsb就是为了cmt,但是spring出现之后,格局就变了,因为程序员又多了一种选择,就是声明式事务管理,声明式事务管理是基于AOP的,及AOP是它的底层特性,本文的目的就是为了和大家探讨一下spring的声明式事务管理,从源代码来分析它的背后的思想。(谢谢异常的建议,因为本文原来没有简介


这个是我昨天在解决问题是看源码得一点体验,可能说得比较大概,希望大家多多讨论,把本贴得质量提高上去,因为spring实现的事务管理这部分我相信还是有点复杂的。一个人未必能想得十分清楚
在spring的声明式事务管理中,它是如何判定一个及标记一个方法是否应该是处在事务体之中呢。

首先要理解的是spring是如何来标记一个方法是否应该处在事务体之中的。有这样一个接口TransactionDefinition,其中定义了很多常量,它还有一个子接口TransactionAttribute,其中只有一个方法rollback。
TransactionDefinition中有很多常量定义,它们分别属于两种类型,传播途径和隔离级别
Java代码 复制代码
  1. /**  
  2.      * Support a current transaction, create a new one if none exists.  
  3.      * Analogous to EJB transaction attribute of the same name.  
  4.      * <p>This is typically the default setting of a transaction definition.  
  5.      */  
  6.     int PROPAGATION_REQUIRED = 0;  
当然其中也定义了隔离级别
/**
Java代码 复制代码
  1. * A constant indicating that dirty reads are prevented; non-repeatable reads   
  2.  * and phantom reads can occur. This level only prohibits a transaction   
  3.  * from reading a row with uncommitted changes in it.   
  4.  * @see java.sql.Connection#TRANSACTION_READ_COMMITTED   
  5.  */   
  6. int ISOLATION_READ_COMMITTED   = Connection.TRANSACTION_READ_COMMITTED;  
同时还有两个对应的方法来得到这样的传播途径和隔离级别
Java代码 复制代码
  1. /**  
  2.      * Return the propagation behavior.  
  3.      * Must return one of the PROPAGATION constants.  
  4.      * @see #PROPAGATION_REQUIRED  
  5.      * @see org.springframework.transaction.support.TransactionSynchronizationManager#isActualTransactionActive()  
  6.      */  
  7.     int getPropagationBehavior();   
  8.   
  9.     /**  
  10.      * Return the isolation level.  
  11.      * Must return one of the ISOLATION constants.  
  12.      * <p>Only makes sense in combination with PROPAGATION_REQUIRED or  
  13.      * PROPAGATION_REQUIRES_NEW.  
  14.      * <p>Note that a transaction manager that does not support custom  
  15.      * isolation levels will throw an exception when given any other level  
  16.      * than ISOLATION_DEFAULT.  
  17.      * @see #ISOLATION_DEFAULT  
  18.      */  
  19.     int getIsolationLevel();  
这个接口有一个默认的实现DefaultTransactionDefinition。然后它还有子类,比如说
DefaultTransactionAttribute。Spring在判断一个方法是否需要事务体的时候其实是创建一个TransactionAttribute实现的实例.

有了上面的简单介绍就可以进入真正判断是否需要事务的地方了。这个方法在TransactionAspectSupport类里,


Java代码 复制代码
  1. /**  
  2.      * Create a transaction if necessary.  
  3.      * @param method method about to execute  
  4.      * @param targetClass class the method is on  
  5.      * @return a TransactionInfo object, whether or not a transaction was created.  
  6.      * The hasTransaction() method on TransactionInfo can be used to tell if there  
  7.      * was a transaction created.  
  8.      */  
  9.     protected TransactionInfo createTransactionIfNecessary(Method method, Class targetClass) {   
  10.         // If the transaction attribute is null, the method is non-transactional.   
  11.         final TransactionAttribute sourceAttr =   
  12.                 this.transactionAttributeSource.getTransactionAttribute(method, targetClass);//就是在这里判断了这个方法的事务属性   
  13.         TransactionAttribute txAttr = sourceAttr;   
  14.   
  15.         // If no name specified, apply method identification as transaction name.   
  16.         if (txAttr != null && txAttr.getName() == null) {   
  17.             final String name = methodIdentification(method);   
  18.             txAttr = new DelegatingTransactionAttribute(sourceAttr) {   
  19.                 public String getName() {   
  20.                     return name;   
  21.                 }   
  22.             };   
  23.         }   
  24.   
  25.         TransactionInfo txInfo = new TransactionInfo(txAttr, method);   
  26. //TransactionInfo是TransactionAspectSupport的一个内部类,它的主要功能是记录方法和对应的事务属性   
  27.         if (txAttr != null) {   
  28.             // We need a transaction for this method   
  29.             if (logger.isDebugEnabled()) {   
  30.                 logger.debug("Getting transaction for " + txInfo.joinpointIdentification());   
  31.             }   
  32.   
  33.             // The transaction manager will flag an error if an incompatible tx already exists   
  34.             txInfo.newTransactionStatus(this.transactionManager.getTransaction(txAttr));//这个方法要仔细的看   
  35.         }   
  36.         else {   
  37.             // The TransactionInfo.hasTransaction() method will return   
  38.             // false. We created it only to preserve the integrity of   
  39.             // the ThreadLocal stack maintained in this class.   
  40.             if (logger.isDebugEnabled())   
  41.                 logger.debug("Don't need to create transaction for [" + methodIdentification(method) +   
  42.                         "]: this method isn't transactional");   
  43.         }   
  44.   
  45.         // We always bind the TransactionInfo to the thread, even if we didn't create   
  46.         // a new transaction here. This guarantees that the TransactionInfo stack   
  47.         // will be managed correctly even if no transaction was created by this aspect.   
  48.         txInfo.bindToThread();   
  49.         return txInfo;   
  50.     }  

TransactionInfo是TransactionAspectSupport的一个内部类,它的主要功能是记录方法和对应的事务属性,在上面这个方法的最后,这个TransactionInfo对象被保存到当前线程中。

而这个方法会在事务拦截器TransactionInterceptor中被调用,TransactionInterceptor实际上是TransactionAspectSupport的子类,看看其中的invoke方法:
Java代码 复制代码
  1. // Work out the target class: may be <code>null</code>.   
  2.         // The TransactionAttributeSource should be passed the target class   
  3.         // as well as the method, which may be from an interface   
  4.         Class targetClass = (invocation.getThis() != null) ? invocation.getThis().getClass() : null;   
  5.            
  6.         // Create transaction if necessary.   
  7.         TransactionInfo txInfo = createTransactionIfNecessary(invocation.getMethod(), targetClass);   
  8.   
  9.         Object retVal = null;   
  10.         try {   
  11.             // This is an around advice.   
  12.             // Invoke the next interceptor in the chain.   
  13.             // This will normally result in a target object being invoked.   
  14.             retVal = invocation.proceed();   
  15.         }   
  16.         catch (Throwable ex) {   
  17.             // target invocation exception   
  18.             doCloseTransactionAfterThrowing(txInfo, ex);   
  19.             throw ex;   
  20.         }   
  21.         finally {   
  22.             doFinally(txInfo);   
  23.         }   
  24.         doCommitTransactionAfterReturning(txInfo);//在这里执行方法结束之后需要的操作   
  25.         return retVal;  

这个方法就如同一般的interceptor需要实现的方法一样。只不过在这个方法里判断被反射的方法是否需要事务。

接着我们重点再回头看一下createTransactionIfNecessary方法里的这一句:
txInfo.newTransactionStatus(this.transactionManager.getTransaction(txAttr));
接着我们就应该去看看这个getTransaction方法了,假设我们是使用hibernate3,其他类似。看getTransaction之前我们来看一下这两类和一个接口
接口PlatformTransactionManager
抽象类public abstract class AbstractPlatformTransactionManager implements PlatformTransactionManager
类public class HibernateTransactionManager extends AbstractPlatformTransactionManager,很明显,这里有一个方法模板模式。
那我们看一下AbstractPlatformTransactionManager中得getTransaction方法:
Java代码 复制代码
  1. public final TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {   
  2.         Object transaction = doGetTransaction();//抽象方法,也需要子类实现,这个方法同样很重要   
  3.   
  4.         // Cache debug flag to avoid repeated checks.   
  5.         boolean debugEnabled = logger.isDebugEnabled();   
  6.         if (debugEnabled) {   
  7.             logger.debug("Using transaction object [" + transaction + "]");   
  8.         }   
  9.   
  10.         if (definition == null) {   
  11.             // Use defaults if no transaction definition given.   
  12.             definition = new DefaultTransactionDefinition();   
  13.         }   
  14.   
  15.         if (isExistingTransaction(transaction)) {   
  16.             // Existing transaction found -> check propagation behavior to find out how to behave.   
  17.             return handleExistingTransaction(definition, transaction, debugEnabled);   
  18.         }   
  19.   
  20.         // Check definition settings for new transaction.   
  21.         if (definition.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {   
  22.             throw new InvalidTimeoutException("Invalid transaction timeout", definition.getTimeout());   
  23.         }   
  24.   
  25.         // No existing transaction found -> check propagation behavior to find out how to behave.   
  26.         if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {   
  27.             throw new IllegalTransactionStateException(   
  28.                     "Transaction propagation 'mandatory' but no existing transaction found");   
  29.         }   
  30.         else if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||   
  31.                 definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||   
  32.             definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {   
  33.             if (debugEnabled) {   
  34.                 logger.debug("Creating new transaction with name [" + definition.getName() + "]");   
  35.             }   
  36.             doBegin(transaction, definition);   
  37.             boolean newSynchronization = (this.transactionSynchronization != SYNCHRONIZATION_NEVER);   
  38.             return newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, null);   
  39.         }   
  40.         else {   
  41.             // Create "empty" transaction: no actual transaction, but potentially synchronization.   
  42.             boolean newSynchronization = (this.transactionSynchronization == SYNCHRONIZATION_ALWAYS);   
  43.             return newTransactionStatus(definition, nullfalse, newSynchronization, debugEnabled, null);   
  44.         }   
  45.     }  
上面的代码很多地方都有解释,所以很好理解,这段代码的关键部分在doBegin(transaction,definition)这里(这是一个抽象方法,子类必须实现这个方法,
具体依赖于抽象,这个是对方法模板模式的一个概括。),前面讲到我们假设是使用hibernate,那么就看看HibernateTransactionManager这个类吧,doBegin里的参数1,transaction其实是HibernateTransactionObject的一个实例,这个实例里主要存放的就是sessionholder,sessionholder里存放的就是开始事务的session和transaction对象,如果之前没有sessionholder存放到线程中,那么这个HibernateTransactionObject的实例的属性其实是空的,这一点可以在doBegin方法的实现中看出来
Java代码 复制代码
  1. protected void doBegin(Object transaction, TransactionDefinition definition) {   
  2.         if (getDataSource() != null && TransactionSynchronizationManager.hasResource(getDataSource())) {   
  3.             throw new IllegalTransactionStateException(   
  4.                     "Pre-bound JDBC Connection found - HibernateTransactionManager does not support " +   
  5.                     "running within DataSourceTransactionManager if told to manage the DataSource itself. " +   
  6.                     "It is recommended to use a single HibernateTransactionManager for all transactions " +   
  7.                     "on a single DataSource, no matter whether Hibernate or JDBC access.");   
  8.         }   
  9.   
  10.         Session session = null;   
  11.   
  12.         try {   
  13.             HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;   
  14.             if (txObject.getSessionHolder() == null) {   
  15.                 Interceptor entityInterceptor = getEntityInterceptor();   
  16.                 Session newSession = (entityInterceptor != null ?   
  17.                         getSessionFactory().openSession(entityInterceptor) : getSessionFactory().openSession());   
  18.                 if (logger.isDebugEnabled()) {   
  19.                     logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction");   
  20.                 }   
  21.                 txObject.setSessionHolder(new SessionHolder(newSession), true);  
}//我们看到,如果传进来的transaction中并没有存放sessionholder,那么就新建一个session,放到新的sessionholder中,再放到HibernateTransactionObject的实例中去,顺便说一下,这个变量的名字取得真是差,虽然是Juergen Hoeller写的,也要批一下,搞得别人会以为是Transaction的实例

Java代码 复制代码
  1. txObject.getSessionHolder().setSynchronizedWithTransaction(true);   
  2.             session = txObject.getSessionHolder().getSession();   
  3.   
  4.             Connection con = session.connection();   
  5.             Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);   
  6.             txObject.setPreviousIsolationLevel(previousIsolationLevel);   
  7.   
  8.             if (definition.isReadOnly() && txObject.isNewSessionHolder()) {   
  9.                 // Just set to NEVER in case of a new Session for this transaction.   
  10.                 session.setFlushMode(FlushMode.NEVER);   
  11.             }//如果是只读事务,并且sessionholder是新建的,那么就设置hibernate的flushmode为never   
  12.   
  13.             if (!definition.isReadOnly() && !txObject.isNewSessionHolder()) {   
  14.                 // We need AUTO or COMMIT for a non-read-only transaction.   
  15.                 FlushMode flushMode = session.getFlushMode();   
  16.                 if (FlushMode.NEVER.equals(flushMode)) {   
  17.                     session.setFlushMode(FlushMode.AUTO);   
  18. //如果session的flushmode是nerver,就设置为auto,因为如果事务定义成非readonly,那么这个session一定是可以flush的   
  19.                     txObject.getSessionHolder().setPreviousFlushMode(flushMode);   
  20.                 }   
  21.             }   
  22.   
  23.             // Add the Hibernate transaction to the session holder.   
  24.             txObject.getSessionHolder().setTransaction(session.beginTransaction());//开始一个事务,并把这个事务对象放到sessionholder中,随后这个sessionholder会通过threadlocal放到线程中,以供在commit时使用   
  25.   
  26.             // Register transaction timeout.   
  27.             if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {   
  28.                 txObject.getSessionHolder().setTimeoutInSeconds(definition.getTimeout());//设置超时时间,如果其超时时间为-1,则不进行设置,如果不是-1,那么超时时间是这样设置的new Date(System.currentTimeMillis() + millis*1000);既程序员在配置文件中指定的其实是秒数   
  29.             }   
  30.   
  31.             // Register the Hibernate Session's JDBC Connection for the DataSource, if set.   
  32.             if (getDataSource() != null) {   
  33.                 ConnectionHolder conHolder = new ConnectionHolder(con);   
  34.                 if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {   
  35.                     conHolder.setTimeoutInSeconds(definition.getTimeout());   
  36.                 }   
  37.                 if (logger.isDebugEnabled()) {   
  38.                     logger.debug("Exposing Hibernate transaction as JDBC transaction [" + con + "]");   
  39.                 }   
  40.                 TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);   
  41.                 txObject.setConnectionHolder(conHolder);   
  42.             }   
  43.   
  44.             // Bind the session holder to the thread.   
  45.             if (txObject.isNewSessionHolder()) {   
  46.                 TransactionSynchronizationManager.bindResource(getSessionFactory(), txObject.getSessionHolder());//如果是新的sessionholder则绑定到线程。这样在进入方法栈中的下一个方法时就能得到整个sessionholder了,connectionholder亦是如此   
  47.             }   
  48.         }   
  49. catch (Exception ex) {   
  50.             SessionFactoryUtils.releaseSession(session, getSessionFactory());//如果抛出异常就释放这个session,这个操作还会在后面出现   
  51.             throw new CannotCreateTransactionException("Could not open Hibernate Session for transaction", ex);   
  52.         }   
  53.     }  
通过以上对代码的注释可以知道,如果给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,这样就会抛这个错了:
Java代码 复制代码
  1. if (method.getName().equals("getCurrentSession")) {   
  2.                 // Handle getCurrentSession method: return transactional Session, if any.   
  3.                 try {   
  4.                     return SessionFactoryUtils.doGetSession((SessionFactory) proxy, false);   
  5. //最后一个参数是false,说明这个方法不能返回一个新的session,没有就抛异常   
  6.                 }   
  7.                 catch (IllegalStateException ex) {   
  8.                     throw new HibernateException(ex.getMessage());   
  9.                 }   
  10.             }  

到这里事务开始部分基本就结束了
按正常流程,那么接下来就是方法结束commit的问题了。

posted on 2008-04-30 11:46 chu 阅读(482) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(2)

随笔档案

我的链接

搜索

最新评论

阅读排行榜

评论排行榜