Spring声明式事务管理源码解读之事务开始与提交.docx
《Spring声明式事务管理源码解读之事务开始与提交.docx》由会员分享,可在线阅读,更多相关《Spring声明式事务管理源码解读之事务开始与提交.docx(37页珍藏版)》请在冰豆网上搜索。
Spring声明式事务管理源码解读之事务开始与提交
Spring声明式事务管理源码解读
简介:
事务是所有企业应用系统的核心,之前人们使用ejb的时候,容器事务管理(CMT),是slsb最令人称道的地方,据说很多人使用ejb,使用slsb就是为了cmt,但是spring出现之后,格局就变了,因为程序员又多了一种选择,就是声明式f事务管理,声明式事务管理是基于AOP的,及AOP是它的底层特性,本文的目的就是为了和大家探讨一下spring的声明式事务管理,从源代码来分析它的背后的思想。
spring声明式事务管理的事务开始部分
这个是我昨天在解决问题是看源码得一点体验,可能说得比较大概,希望大家多多讨论,把本贴得质量提高上去,因为spring实现的事务管理这部分我相信还是有点复杂的。
一个人未必能想得十分清楚
在spring的声明式事务管理中,它是如何判定一个及标记一个方法是否应该是处在事务体之中呢。
首先要理解的是spring是如何来标记一个方法是否应该处在事务体之中的。
有这样一个接口TransactionDefinition,其中定义了很多常量,它还有一个子接口TransactionAttribute,其中只有一个方法rollback。
TransactionDefinition中有很多常量定义,它们分别属于两种类型,传播途径和隔离级别
Java代码
1./**
2.*Supportacurrenttransaction,createanewoneifnoneexists.
3.*AnalogoustoEJBtransactionattributeofthesamename.
4.*
Thisistypicallythedefaultsettingofatransactiondefinition.
5.*/
6.intPROPAGATION_REQUIRED=0;
/**
*Supportacurrenttransaction,createanewoneifnoneexists.
*AnalogoustoEJBtransactionattributeofthesamename.
*
Thisistypicallythedefaultsettingofatransactiondefinition.
*/
intPROPAGATION_REQUIRED=0;
当然其中也定义了隔离级别
/**
Java代码
1.*Aconstantindicatingthatdirtyreadsareprevented;non-repeatablereads
2.*andphantomreadscanoccur.Thislevelonlyprohibitsatransaction
3.*fromreadingarowwithuncommittedchangesinit.
4.*@seejava.sql.Connection#TRANSACTION_READ_COMMITTED
5.*/
6.intISOLATION_READ_COMMITTED=Connection.TRANSACTION_READ_COMMITTED;
*Aconstantindicatingthatdirtyreadsareprevented;non-repeatablereads
*andphantomreadscanoccur.Thislevelonlyprohibitsatransaction
*fromreadingarowwithuncommittedchangesinit.
*@seejava.sql.Connection#TRANSACTION_READ_COMMITTED
*/
intISOLATION_READ_COMMITTED=Connection.TRANSACTION_READ_COMMITTED;
同时还有两个对应的方法来得到这样的传播途径和隔离级别
Java代码
1./**
2.*Returnthepropagationbehavior.
3.*MustreturnoneofthePROPAGATIONconstants.
4.*@see#PROPAGATION_REQUIRED
5.*@seeorg.springframework.transaction.support.TransactionSynchronizationManager#isActualTransactionActive()
6.*/
7.intgetPropagationBehavior();
8.
9./**
10.*Returntheisolationlevel.
11.*MustreturnoneoftheISOLATIONconstants.
12.*
OnlymakessenseincombinationwithPROPAGATION_REQUIREDor
13.*PROPAGATION_REQUIRES_NEW.
14.*
Notethatatransactionmanagerthatdoesnotsupportcustom
15.*isolationlevelswillthrowanexceptionwhengivenanyotherlevel
16.*thanISOLATION_DEFAULT.
17.*@see#ISOLATION_DEFAULT
18.*/
19.intgetIsolationLevel();
/**
*Returnthepropagationbehavior.
*MustreturnoneofthePROPAGATIONconstants.
*@see#PROPAGATION_REQUIRED
*@seeorg.springframework.transaction.support.TransactionSynchronizationManager#isActualTransactionActive()
*/
intgetPropagationBehavior();
/**
*Returntheisolationlevel.
*MustreturnoneoftheISOLATIONconstants.
*
OnlymakessenseincombinationwithPROPAGATION_REQUIREDor
*PROPAGATION_REQUIRES_NEW.
*
Notethatatransactionmanagerthatdoesnotsupportcustom
*isolationlevelswillthrowanexceptionwhengivenanyotherlevel
*thanISOLATION_DEFAULT.
*@see#ISOLATION_DEFAULT
*/
intgetIsolationLevel();
这个接口有一个默认的实现DefaultTransactionDefinition。
然后它还有子类,比如说
DefaultTransactionAttribute。
Spring在判断一个方法是否需要事务体的时候其实是创建一个TransactionAttribute实现的实例.
有了上面的简单介绍就可以进入真正判断是否需要事务的地方了。
这个方法在TransactionAspectSupport类里,
Java代码
1./**
2.*Createatransactionifnecessary.
3.*@parammethodmethodabouttoexecute
4.*@paramtargetClassclassthemethodison
5.*@returnaTransactionInfoobject,whetherornotatransactionwascreated.
6.*ThehasTransaction()methodonTransactionInfocanbeusedtotellifthere
7.*wasatransactioncreated.
8.*/
9.protectedTransactionInfocreateTransactionIfNecessary(Methodmethod,ClasstargetClass){
10.//Ifthetransactionattributeisnull,themethodisnon-transactional.
11.finalTransactionAttributesourceAttr=
12.this.transactionAttributeSource.getTransactionAttribute(method,targetClass);//就是在这里判断了这个方法的事务属性
13.TransactionAttributetxAttr=sourceAttr;
14.
15.//Ifnonamespecified,applymethodidentificationastransactionname.
16.if(txAttr!
=null&&txAttr.getName()==null){
17.finalStringname=methodIdentification(method);
18.txAttr=newDelegatingTransactionAttribute(sourceAttr){
19.publicStringgetName(){
20.returnname;
21.}
22.};
23.}
24.
25.TransactionInfotxInfo=newTransactionInfo(txAttr,method);
26.//TransactionInfo是TransactionAspectSupport的一个内部类,它的主要功能是记录方法和对应的事务属性
27.if(txAttr!
=null){
28.//Weneedatransactionforthismethod
29.if(logger.isDebugEnabled()){
30.logger.debug("Gettingtransactionfor"+txInfo.joinpointIdentification());
31.}
32.
33.//Thetransactionmanagerwillflagan