梦之天堂

我学故我知,我思故我在;java你我,happy你我——sylilzy

BlogJava 首页 新随笔 联系 聚合 管理
  3 Posts :: 8 Stories :: 2 Comments :: 0 Trackbacks
spring的事务处理详解:事务创建
 
sylilzy@163.com
 
在配置spring的事务处理时,无论使用TransactionProxyFactoryBean,还是使用BeanNameAutoProxyCreator
spring的事务处理都是主要由TransactionInterceptor来拦截完成,此类扩展自org.aopalliance.intercept.MethodInterceptor,要查看spring的事务处理过程,首先要了解
TransactionInterceptor类的执行过程:
 
 
 
1.事务拦截器拦截调用方法:invoke();
2.调用TransactionAspectSupport的createTransactionIfNecessary,
 
 
createTransactionIfNecessary中:
首先创建TransactionInfo对象,然后
如果被调用的方法设置了事务属性(不管是什么属性,只要设置了),输出日志:
 
TransactionInterceptor 221 - Getting transaction for ...
 
并调用TransactionManager.getTransaction方法,
 
如果被调用的方法设置未设备事务,输出:Don't need to create transaction for ...
 
调用返回后然后将事务绑定到当前线程.createTransactionIfNecessary方法返回.
 
3.调用目标类的方法,并依次完成其它拦截器的调用
4.如果在上一步操作中有异常抛出,则会处理异常,处理过程:根据配置决定是提交还是回滚事务
5.如无异常,调用doFinally()将上一个事务(旧事务)设置为当前事务
4.调用doCommitTransactionAfterReturning提交事务,此为最重要的一步,与事务相关的操作在此实际生效.
 
调用TransactionManager.getTransaction过程 :
1.调用doGetTransaction()查找事务(此对象并不代码一个事务已经存在),返回的对象中包含事务的相关信息:如事务是否开始等.此对象将在以后作为doBegin and doCommit等方法的参数.
 
2.输出日志:
Using transaction object...
如:
HibernateTransactionManager 254 - Using transaction object...
 
3.调用isExistingTransaction(...)检查事务是否存在(是否已经开始一个事务),此方法为abstract方法,需要concreate类来实现,例如hibernate
 
4.如果isExistingTransaction为true,
如果是PROPAGATION_NEVER,则抛异常
PROPAGATION_NOT_SUPPORTED,则suspend当前事务并返回
PROPAGATION_REQUIRES_NEW,则suspend后创建一个新事务,
其它则
输出日志:"Participating in existing transaction"
然后
处理完后返回一个TransactionStatus对象,包含是否为新transaction,是否为新的newSynchronization,suspendedResources等,getTransaction()同时也返回
 
5. 如果isExistingTransaction为false
检查超时是否小于默认时间,如果是则抛异常
如果当前方法的事务属性为PROPAGATION_MANDATORY,则抛异常,否则
如果当前方法的事务属性为PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW,PROPAGATION_NESTED
输出日志:"Creating new transaction with name ..."
调用doBegin(...)创建并开始一个事务,然后返回
否则返回的return newTransactionStatus(definition, null, false, newSynchronization, debugEnabled, null);

作者简介:
    施祖阳,网名sylilzy,1979年生。
    2002年起从事软件开发工作,主要研究为JAVA、Linux及相关技术。
    你可通过sylilzy@163.com与作者联系。

posted on 2006-06-14 18:01 sylilzy 阅读(3460) 评论(0)  编辑  收藏 所属分类: Java

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


网站导航: