当前位置: 移动技术网 > IT编程>开发语言>Java > spring5 源码深度解析----- 事务增强器(100%理解事务)

spring5 源码深度解析----- 事务增强器(100%理解事务)

2019年10月15日  | 移动技术网IT编程  | 我要评论
上一篇文章我们讲解了事务的Advisor是如何注册进Spring容器的,也讲解了Spring是如何将有配置事务的类配置上事务的,实际上也就是用了AOP那一套,也讲解了Advisor,pointcut验证流程,至此,事务的初始化工作都已经完成了,在之后的调用过程,如果代理类的方法被调用,都会调用Bea ...

上一篇文章我们讲解了事务的advisor是如何注册进spring容器的,也讲解了spring是如何将有配置事务的类配置上事务的,实际上也就是用了aop那一套,也讲解了advisor,pointcut验证流程,至此,事务的初始化工作都已经完成了,在之后的调用过程,如果代理类的方法被调用,都会调用beanfactorytransactionattributesourceadvisor这个advisor的增强方法,也就是我们还未提到的那个advisor里面的advise,还记得吗,在自定义标签的时候我们将transactioninterceptor这个advice作为bean注册进ioc容器,并且将其注入进advisor中,这个advice在代理类的invoke方法中会被封装到拦截器链中,最终事务的功能都在advise中体现,所以我们先来关注一下transactioninterceptor这个类吧。

transactioninterceptor类继承自methodinterceptor,所以调用该类是从其invoke方法开始的,首先预览下这个方法:

@override
@nullable
public object invoke(final methodinvocation invocation) throws throwable {
    // work out the target class: may be {@code null}.
    // the transactionattributesource should be passed the target class
    // as well as the method, which may be from an interface.
    class<?> targetclass = (invocation.getthis() != null ? aoputils.gettargetclass(invocation.getthis()) : null);

    // adapt to transactionaspectsupport's invokewithintransaction...
    return invokewithintransaction(invocation.getmethod(), targetclass, invocation::proceed);
}

重点来了,进入invokewithintransaction方法:

@nullable
protected object invokewithintransaction(method method, @nullable class<?> targetclass,
        final invocationcallback invocation) throws throwable {

    // if the transaction attribute is null, the method is non-transactional.
    transactionattributesource tas = gettransactionattributesource();
    // 获取对应事务属性
    final transactionattribute txattr = (tas != null ? tas.gettransactionattribute(method, targetclass) : null);
    // 获取beanfactory中的transactionmanager
    final platformtransactionmanager tm = determinetransactionmanager(txattr);
    // 构造方法唯一标识(类.方法,如:service.userserviceimpl.save)
    final string joinpointidentification = methodidentification(method, targetclass, txattr);

    // 声明式事务处理
    if (txattr == null || !(tm instanceof callbackpreferringplatformtransactionmanager)) {
        // 创建transactioninfo
        transactioninfo txinfo = createtransactionifnecessary(tm, txattr, joinpointidentification);
        object retval = null;
        try {
            // 执行原方法
            // 继续调用方法拦截器链,这里一般将会调用目标类的方法,如:accountserviceimpl.save方法
            retval = invocation.proceedwithinvocation();
        }
        catch (throwable ex) {
            // 异常回滚
            completetransactionafterthrowing(txinfo, ex);
            // 手动向上抛出异常,则下面的提交事务不会执行
            // 如果子事务出异常,则外层事务代码需catch住子事务代码,不然外层事务也会回滚
            throw ex;
        }
        finally {
            // 消除信息
            cleanuptransactioninfo(txinfo);
        }
        // 提交事务
        committransactionafterreturning(txinfo);
        return retval;
    }

    else {
        final throwableholder throwableholder = new throwableholder();
        try {
            // 编程式事务处理
            object result = ((callbackpreferringplatformtransactionmanager) tm).execute(txattr, status -> {
                transactioninfo txinfo = preparetransactioninfo(tm, txattr, joinpointidentification, status);
                try {
                    return invocation.proceedwithinvocation();
                }
                catch (throwable ex) {
                    if (txattr.rollbackon(ex)) {
                        // a runtimeexception: will lead to a rollback.
                        if (ex instanceof runtimeexception) {
                            throw (runtimeexception) ex;
                        }
                        else {
                            throw new throwableholderexception(ex);
                        }
                    }
                    else {
                        // a normal return value: will lead to a commit.
                        throwableholder.throwable = ex;
                        return null;
                    }
                }
                finally {
                    cleanuptransactioninfo(txinfo);
                }
            });

            // check result state: it might indicate a throwable to rethrow.
            if (throwableholder.throwable != null) {
                throw throwableholder.throwable;
            }
            return result;
        }
        catch (throwableholderexception ex) {
            throw ex.getcause();
        }
        catch (transactionsystemexception ex2) {
            if (throwableholder.throwable != null) {
                logger.error("application exception overridden by commit exception", throwableholder.throwable);
                ex2.initapplicationexception(throwableholder.throwable);
            }
            throw ex2;
        }
        catch (throwable ex2) {
            if (throwableholder.throwable != null) {
                logger.error("application exception overridden by commit exception", throwableholder.throwable);
            }
            throw ex2;
        }
    }
}

创建事务info对象

我们先分析事务创建的过程。

protected transactioninfo createtransactionifnecessary(@nullable platformtransactionmanager tm,
        @nullable transactionattribute txattr, final string joinpointidentification) {

    // if no name specified, apply method identification as transaction name.
    // 如果没有名称指定则使用方法唯一标识,并使用delegatingtransactionattribute封装txattr
    if (txattr != null && txattr.getname() == null) {
        txattr = new delegatingtransactionattribute(txattr) {
            @override
            public string getname() {
                return joinpointidentification;
            }
        };
    }

    transactionstatus status = null;
    if (txattr != null) {
        if (tm != null) {
            // 获取transactionstatus
            status = tm.gettransaction(txattr);
        }
        else {
            if (logger.isdebugenabled()) {
                logger.debug("skipping transactional joinpoint [" + joinpointidentification +
                        "] because no transaction manager has been configured");
            }
        }
    }
    // 根据指定的属性与status准备一个transactioninfo
    return preparetransactioninfo(tm, txattr, joinpointidentification, status);
}

对于createtransactionlfnecessary函数主要做了这样几件事情。

(1)使用 delegatingtransactionattribute 封装传入的 transactionattribute 实例。

对于传入的transactionattribute类型的参数txattr,当前的实际类型是rulebasedtransactionattribute,是由获取事务属性时生成,主要用于数据承载,而这里之所以使用delegatingtransactionattribute进行封装,当然是提供了更多的功能。

(2)获取事务。

事务处理当然是以事务为核心,那么获取事务就是最重要的事情。

(3)构建事务信息。

根据之前几个步骤获取的信息构建transactionlnfo并返回。

获取事务

其中核心是在gettransaction方法中:

@override
public final transactionstatus gettransaction(@nullable transactiondefinition definition) throws transactionexception {
    // 获取一个transaction
    object transaction = dogettransaction();

    boolean debugenabled = logger.isdebugenabled();

    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());
    }

    // 走到这里说明此时没有存在事务,如果传播特性是mandatory时抛出异常
    if (definition.getpropagationbehavior() == transactiondefinition.propagation_mandatory) {
        throw new illegaltransactionstateexception(
                "no existing transaction found for transaction marked with propagation 'mandatory'");
    }
    // 如果此时不存在事务,当传播特性是required或new或nested都会进入if语句块
    else if (definition.getpropagationbehavior() == transactiondefinition.propagation_required ||
            definition.getpropagationbehavior() == transactiondefinition.propagation_requires_new ||
            definition.getpropagationbehavior() == transactiondefinition.propagation_nested) {
        // propagation_required、propagation_requires_new、propagation_nested都需要新建事务
        // 因为此时不存在事务,将null挂起
        suspendedresourcesholder suspendedresources = suspend(null);
        if (debugenabled) {
            logger.debug("creating new transaction with name [" + definition.getname() + "]: " + definition);
        }
        try {
            boolean newsynchronization = (gettransactionsynchronization() != synchronization_never);
            // new一个status,存放刚刚创建的transaction,然后将其标记为新事务!
            // 这里transaction后面一个参数决定是否是新事务!
            defaulttransactionstatus status = newtransactionstatus(
                    definition, transaction, true, newsynchronization, debugenabled, suspendedresources);
            // 新开一个连接的地方,非常重要
            dobegin(transaction, definition);
            preparesynchronization(status, definition);
            return status;
        }
        catch (runtimeexception | error ex) {
            resume(null, suspendedresources);
            throw ex;
        }
    }
    else {
        // create "empty" transaction: no actual transaction, but potentially synchronization.
        if (definition.getisolationlevel() != transactiondefinition.isolation_default && logger.iswarnenabled()) {
            logger.warn("custom isolation level specified but no actual transaction initiated; " +
                    "isolation level will effectively be ignored: " + definition);
        }
        // 其他的传播特性一律返回一个空事务,transaction = null
        //当前不存在事务,且传播机制=propagation_supports/propagation_not_supported/propagation_never,这三种情况,创建“空”事务
        boolean newsynchronization = (gettransactionsynchronization() == synchronization_always);
        return preparetransactionstatus(definition, null, true, newsynchronization, debugenabled, null);
    }
}

先来看看transaction是如何被创建出来的:

@override
protected object dogettransaction() {
    // 这里datasourcetransactionobject是事务管理器的一个内部类
    // datasourcetransactionobject就是一个transaction,这里new了一个出来
    datasourcetransactionobject txobject = new datasourcetransactionobject();
    txobject.setsavepointallowed(isnestedtransactionallowed());
    // 解绑与绑定的作用在此时体现,如果当前线程有绑定的话,将会取出holder
    // 第一次conholder肯定是null
    connectionholder conholder =
    (connectionholder) transactionsynchronizationmanager.getresource(obtaindatasource());
    // 此时的holder被标记成一个旧holder
    txobject.setconnectionholder(conholder, false);
    return txobject;
}

创建transaction过程很简单,接着就会判断当前是否存在事务:

@override
protected boolean isexistingtransaction(object transaction) {
    datasourcetransactionobject txobject = (datasourcetransactionobject) transaction;
    return (txobject.hasconnectionholder() && txobject.getconnectionholder().istransactionactive());
}

public boolean hasconnectionholder() {
    return (this.connectionholder != null);
}

这里判断是否存在事务的依据主要是获取holder中的transactionactive变量是否为true,如果是第一次进入事务,holder直接为null判断不存在了,如果是第二次进入事务transactionactive变量是为true的(后面会提到是在哪里把它变成true的),由此来判断当前是否已经存在事务了。

至此,源码分成了2条处理线

1.当前已存在事务:isexistingtransaction()判断是否存在事务,存在事务handleexistingtransaction()根据不同传播机制不同处理

2.当前不存在事务: 不同传播机制不同处理

当前不存在事务

如果不存在事务,传播特性又是required或new或nested,将会先挂起null,这个挂起方法我们后面再讲,然后创建一个defaulttransactionstatus ,并将其标记为新事务,然后执行dobegin(transaction, definition);这个方法也是一个关键方法

神秘又关键的status对象

transactionstatus接口

public interface transactionstatus extends savepointmanager, flushable {
    // 返回当前事务是否为新事务(否则将参与到现有事务中,或者可能一开始就不在实际事务中运行)
    boolean isnewtransaction();
    // 返回该事务是否在内部携带保存点,也就是说,已经创建为基于保存点的嵌套事务。
    boolean hassavepoint();
    // 设置事务仅回滚。
    void setrollbackonly();
    // 返回事务是否已标记为仅回滚
    boolean isrollbackonly();
    // 将会话刷新到数据存储区
    @override
    void flush();
    // 返回事物是否已经完成,无论提交或者回滚。
    boolean iscompleted();
}

 

再来看看实现类defaulttransactionstatus

defaulttransactionstatus

public class defaulttransactionstatus extends abstracttransactionstatus {

    //事务对象
    @nullable
    private final object transaction;

    //事务对象
    private final boolean newtransaction;

    private final boolean newsynchronization;

    private final boolean readonly;

    private final boolean debug;

    //事务对象
    @nullable
    private final object suspendedresources;
    
        public defaulttransactionstatus(
            @nullable object transaction, boolean newtransaction, boolean newsynchronization,
            boolean readonly, boolean debug, @nullable object suspendedresources) {

        this.transaction = transaction;
        this.newtransaction = newtransaction;
        this.newsynchronization = newsynchronization;
        this.readonly = readonly;
        this.debug = debug;
        this.suspendedresources = suspendedresources;
    }
    
    //略...
}

 

我们看看这行代码 defaulttransactionstatus status = newtransactionstatus( definition, transaction, true, newsynchronization, debugenabled, suspendedresources);

// 这里是构造一个status对象的方法
protected defaulttransactionstatus newtransactionstatus(
    transactiondefinition definition, @nullable object transaction, boolean newtransaction,
    boolean newsynchronization, boolean debug, @nullable object suspendedresources) {

    boolean actualnewsynchronization = newsynchronization &&
        !transactionsynchronizationmanager.issynchronizationactive();
    return new defaulttransactionstatus(
        transaction, newtransaction, actualnewsynchronization,
        definition.isreadonly(), debug, suspendedresources);
}

 

实际上就是封装了事务属性definition,新创建的transaction,并且将事务状态属性设置为新事务,最后一个参数为被挂起的事务。

简单了解一下关键参数即可:

第二个参数transaction:事务对象,在一开头就有创建,其就是事务管理器的一个内部类。
第三个参数newtransaction:布尔值,一个标识,用于判断是否是新的事务,用于提交或者回滚方法中,是新的才会提交或者回滚。
最后一个参数suspendedresources:被挂起的对象资源,挂起操作会返回旧的holder,将其与一些事务属性一起封装成一个对象,就是这个suspendedresources这个对象了,它会放在status中,在最后的清理工作方法中判断status中是否有这个挂起对象,如果有会恢复它

接着我们来看看关键代码 dobegin(transaction, definition);

 1 @override
 2 protected void dobegin(object transaction, transactiondefinition definition) {
 3     datasourcetransactionobject txobject = (datasourcetransactionobject) transaction;
 4     connection con = null;
 5 
 6     try {
 7         // 判断如果transaction没有holder的话,才去从datasource中获取一个新连接
 8         if (!txobject.hasconnectionholder() ||
 9                 txobject.getconnectionholder().issynchronizedwithtransaction()) {
10             //通过datasource获取连接
11             connection newcon = this.datasource.getconnection();
12             if (logger.isdebugenabled()) {
13                 logger.debug("acquired connection [" + newcon + "] for jdbc transaction");
14             }
15             // 所以,只有transaction中的holder为空时,才会设置为新holder
16             // 将获取的连接封装进connectionholder,然后封装进transaction的connectionholder属性
17             txobject.setconnectionholder(new connectionholder(newcon), true);
18         }
19      //设置新的连接为事务同步中
20         txobject.getconnectionholder().setsynchronizedwithtransaction(true);
21         con = txobject.getconnectionholder().getconnection();
22      //conn设置事务隔离级别,只读
23         integer previousisolationlevel = datasourceutils.prepareconnectionfortransaction(con, definition);
24         txobject.setpreviousisolationlevel(previousisolationlevel);//datasourcetransactionobject设置事务隔离级别
25 
26         // 如果是自动提交切换到手动提交
27         if (con.getautocommit()) {
28             txobject.setmustrestoreautocommit(true);
29             if (logger.isdebugenabled()) {
30                 logger.debug("switching jdbc connection [" + con + "] to manual commit");
31             }
32             con.setautocommit(false);
33         }
34      // 如果只读,执行sql设置事务只读
35         preparetransactionalconnection(con, definition);
36         // 设置connection持有者的事务开启状态
37         txobject.getconnectionholder().settransactionactive(true);
38 
39         int timeout = determinetimeout(definition);
40         if (timeout != transactiondefinition.timeout_default) {
41             // 设置超时秒数
42             txobject.getconnectionholder().settimeoutinseconds(timeout);
43         }
44 
45         // 将当前获取到的连接绑定到当前线程
46         if (txobject.isnewconnectionholder()) {
47             transactionsynchronizationmanager.bindresource(getdatasource(), txobject.getconnectionholder());
48         }
49     }catch (throwable ex) {
50         if (txobject.isnewconnectionholder()) {
51             datasourceutils.releaseconnection(con, this.datasource);
52             txobject.setconnectionholder(null, false);
53         }
54         throw new cannotcreatetransactionexception("could not open jdbc connection for transaction", ex);
55     }
56 }

 

conn设置事务隔离级别,只读

@nullable
public static integer prepareconnectionfortransaction(connection con, @nullable transactiondefinition definition)
        throws sqlexception {

    assert.notnull(con, "no connection specified");

    // set read-only flag.
    // 设置数据连接的只读标识
    if (definition != null && definition.isreadonly()) {
        try {
            if (logger.isdebugenabled()) {
                logger.debug("setting jdbc connection [" + con + "] read-only");
            }
            con.setreadonly(true);
        }
        catch (sqlexception | runtimeexception ex) {
            throwable extocheck = ex;
            while (extocheck != null) {
                if (extocheck.getclass().getsimplename().contains("timeout")) {
                    // assume it's a connection timeout that would otherwise get lost: e.g. from jdbc 4.0
                    throw ex;
                }
                extocheck = extocheck.getcause();
            }
            // "read-only not supported" sqlexception -> ignore, it's just a hint anyway
            logger.debug("could not set jdbc connection read-only", ex);
        }
    }

    // apply specific isolation level, if any.
    // 设置数据库连接的隔离级别
    integer previousisolationlevel = null;
    if (definition != null && definition.getisolationlevel() != transactiondefinition.isolation_default) {
        if (logger.isdebugenabled()) {
            logger.debug("changing isolation level of jdbc connection [" + con + "] to " +
                    definition.getisolationlevel());
        }
        int currentisolation = con.gettransactionisolation();
        if (currentisolation != definition.getisolationlevel()) {
            previousisolationlevel = currentisolation;
            con.settransactionisolation(definition.getisolationlevel());
        }
    }

    return previousisolationlevel;
}

 

我们看到都是通过 connection 去设置

线程变量的绑定

我们看 dobegin 方法的47行,将当前获取到的连接绑定到当前线程,绑定与解绑围绕一个线程变量,此变量在transactionsynchronizationmanager类中:

private static final threadlocal<map<object, object>> resources =  new namedthreadlocal<>("transactional resources");

 

这是一个 static final 修饰的 线程变量,存储的是一个map,我们来看看47行的静态方法,bindresource

public static void bindresource(object key, object value) throws illegalstateexception {
    // 从上面可知,线程变量是一个map,而这个key就是datasource
    // 这个value就是holder
    object actualkey = transactionsynchronizationutils.unwrapresourceifnecessary(key);
    assert.notnull(value, "value must not be null");
    // 获取这个线程变量map
    map<object, object> map = resources.get();
    // set threadlocal map if none found
    if (map == null) {
        map = new hashmap<>();
        resources.set(map);
    }
    // 将新的holder作为value,datasource作为key放入当前线程map中
    object oldvalue = map.put(actualkey, value);
    // transparently suppress a resourceholder that was marked as void...
    if (oldvalue instanceof resourceholder && ((resourceholder) oldvalue).isvoid()) {
        oldvalue = null;
    }
    if (oldvalue != null) {
        throw new illegalstateexception("already value [" + oldvalue + "] for key [" +
                actualkey + "] bound to thread [" + thread.currentthread().getname() + "]");
    }    thread.currentthread().getname() + "]");
    }
    // 略...
}

 

扩充知识点

这里再扩充一点,mybatis中获取的数据库连接,就是根据 datasource 从threadlocal中获取的

以查询举例,会调用executor#doquery方法:

 

 最终会调用datasourceutils#dogetconnection获取,真正的数据库连接,其中transactionsynchronizationmanager中保存的就是方法调用前,spring增强方法中绑定到线程的connection,从而保证整个事务过程中connection的一致性

 

 

 

我们看看transactionsynchronizationmanager.getresource(object key)这个方法

@nullable
public static object getresource(object key) {
    object actualkey = transactionsynchronizationutils.unwrapresourceifnecessary(key);
    object value = dogetresource(actualkey);
    if (value != null && logger.istraceenabled()) {
        logger.trace("retrieved value [" + value + "] for key [" + actualkey + "] bound to thread [" +
                thread.currentthread().getname() + "]");
    }
    return value;
}

    @nullable
private static object dogetresource(object actualkey) {
    map<object, object> map = resources.get();
    if (map == null) {
        return null;
    }
    object value = map.get(actualkey);
    // transparently remove resourceholder that was marked as void...
    if (value instanceof resourceholder && ((resourceholder) value).isvoid()) {
        map.remove(actualkey);
        // remove entire threadlocal if empty...
        if (map.isempty()) {
            resources.remove();
        }
        value = null;
    }
    return value;
}

就是从线程变量的map中根据 datasource获取 connectionholder

已经存在的事务

前面已经提到,第一次事务开始时必会新创一个holder然后做绑定操作,此时线程变量是有holder的且avtive为true,如果第二个事务进来,去new一个transaction之后去线程变量中取holder,holder是不为空的且active是为true的,所以会进入handleexistingtransaction方法:

 1 private transactionstatus handleexistingtransaction(
 2         transactiondefinition definition, object transaction, boolean debugenabled)
 3         throws transactionexception {
 4    // 1.nerver(不支持当前事务;如果当前事务存在,抛出异常)报错
 5     if (definition.getpropagationbehavior() == transactiondefinition.propagation_never) {
 6         throw new illegaltransactionstateexception(
 7                 "existing transaction found for transaction marked with propagation 'never'");
 8     }
 9    // 2.not_supported(不支持当前事务,现有同步将被挂起)挂起当前事务,返回一个空事务
10     if (definition.getpropagationbehavior() == transactiondefinition.propagation_not_supported) {
11         if (debugenabled) {
12             logger.debug("suspending current transaction");
13         }
14         // 这里会将原来的事务挂起,并返回被挂起的对象
15         object suspendedresources = suspend(transaction);
16         boolean newsynchronization = (gettransactionsynchronization() == synchronization_always);
17         // 这里可以看到,第二个参数transaction传了一个空事务,第三个参数false为旧标记
18         // 最后一个参数就是将前面挂起的对象封装进新的status中,当前事务执行完后,就恢复suspendedresources
19         return preparetransactionstatus(definition, null, false, newsynchronization, debugenabled, suspendedresources);
20     }
21    // 3.requires_new挂起当前事务,创建新事务
22     if (definition.getpropagationbehavior() == transactiondefinition.propagation_requires_new) {
23         if (debugenabled) {
24             logger.debug("suspending current transaction, creating new transaction with name [" +
25                     definition.getname() + "]");
26         }
27         // 将原事务挂起,此时新建事务,不与原事务有关系
28         // 会将transaction中的holder设置为null,然后解绑!
29         suspendedresourcesholder suspendedresources = suspend(transaction);
30         try {
31             boolean newsynchronization = (gettransactionsynchronization() != synchronization_never);
32             // new一个status出来,传入transaction,并且为新事务标记,然后传入挂起事务
33             defaulttransactionstatus status = newtransactionstatus(definition, transaction, true, newsynchronization, debugenabled, suspendedresources);
34             // 这里也做了一次dobegin,此时的transaction中holer是为空的,因为之前的事务被挂起了
35             // 所以这里会取一次新的连接,并且绑定!
36             dobegin(transaction, definition);
37             preparesynchronization(status, definition);
38             return status;
39         }
40         catch (runtimeexception beginex) {
41             resumeafterbeginexception(transaction, suspendedresources, beginex);
42             throw beginex;
43         }
44         catch (error beginerr) {
45             resumeafterbeginexception(transaction, suspendedresources, beginerr);
46             throw beginerr;
47         }
48     }
49   // 如果此时的传播特性是nested,不会挂起事务
50     if (definition.getpropagationbehavior() == transactiondefinition.propagation_nested) {
51         if (!isnestedtransactionallowed()) {
52             throw new nestedtransactionnotsupportedexception(
53                     "transaction manager does not allow nested transactions by default - " +
54                     "specify 'nestedtransactionallowed' property with value 'true'");
55         }
56         if (debugenabled) {
57             logger.debug("creating nested transaction with name [" + definition.getname() + "]");
58         }
59         // 这里如果是jta事务管理器,就不可以用savepoint了,将不会进入此方法
60         if (usesavepointfornestedtransaction()) { 
61             // 这里不会挂起事务,说明nested的特性是原事务的子事务而已
62             // new一个status,传入transaction,传入旧事务标记,传入挂起对象=null
63             defaulttransactionstatus status =preparetransactionstatus(definition, transaction, false, false, debugenabled, null);
64             // 这里是nested特性特殊的地方,在先前存在事务的情况下会建立一个savepoint
65             status.createandholdsavepoint();
66             return status;
67         }
68         else {
69             // jta事务走这个分支,创建新事务
70             boolean newsynchronization = (gettransactionsynchronization() != synchronization_never);
71             defaulttransactionstatus status = newtransactionstatus(
72                     definition, transaction, true, newsynchronization, debugenabled, null);
73             dobegin(transaction, definition);
74             preparesynchronization(status, definition);
75             return status;
76         }
77     }
78 
79     // 到这里propagation_supports 或 propagation_required或propagation_mandatory,存在事务加入事务即可,标记为旧事务,空挂起
80     boolean newsynchronization = (gettransactionsynchronization() != synchronization_never);
81     return preparetransactionstatus(definition, transaction, false, newsynchronization, debugenabled, null);
82 }

 

对于已经存在事务的处理过程中,我们看到了很多熟悉的操作,但是,也有些不同的地方,函数中对已经存在的事务处理考虑两种情况。

(1)propagation_requires_new表示当前方法必须在它自己的事务里运行,一个新的事务将被启动,而如果有一个事务正在运行的话,则在这个方法运行期间被挂起。而spring中对于此种传播方式的处理与新事务建立最大的不同点在于使用suspend方法将原事务挂起。 将信息挂起的目的当然是为了在当前事务执行完毕后在将原事务还原。

(2)propagation_nested表示如果当前正有一个事务在运行中,则该方法应该运行在一个嵌套的事务中,被嵌套的事务可以独立于封装事务进行提交或者回滚,如果封装事务不存在,行为就像propagation_requires_new。对于嵌入式事务的处理,spring中主要考虑了两种方式的处理。

  • spring中允许嵌入事务的时候,则首选设置保存点的方式作为异常处理的回滚。
  • 对于其他方式,比如jta无法使用保存点的方式,那么处理方式与propagation_ requires_new相同,而一旦出现异常,则由spring的事务异常处理机制去完成后续操作。

对于挂起操作的主要目的是记录原有事务的状态,以便于后续操作对事务的恢复

小结

到这里我们可以知道,在当前存在事务的情况下,根据传播特性去决定是否为新事务,是否挂起当前事务。

not_supported :会挂起事务,不运行dobegin方法传空transaction,标记为旧事务。封装status对象:

return preparetransactionstatus(definition, null, false, newsynchronization, debugenabled, suspendedresources)

 

requires_new :将会挂起事务且运行dobegin方法,标记为新事务。封装status对象:

defaulttransactionstatus status = newtransactionstatus(definition, transaction, true, newsynchronization, debugenabled, suspendedresources);

nested :不会挂起事务且不会运行dobegin方法,标记为旧事务,但会创建savepoint。封装status对象:

defaulttransactionstatus status =preparetransactionstatus(definition, transaction, false, false, debugenabled, null);

其他事务例如required :不会挂起事务,封装原有的transaction不会运行dobegin方法,标记旧事务,封装status对象:

return preparetransactionstatus(definition, transaction, false, newsynchronization, debugenabled, null);

 

挂起

对于挂起操作的主要目的是记录原有事务的状态,以便于后续操作对事务的恢复:

@nullable
protected final suspendedresourcesholder suspend(@nullable object transaction) throws transactionexception {
    if (transactionsynchronizationmanager.issynchronizationactive()) {
        list<transactionsynchronization> suspendedsynchronizations = dosuspendsynchronization();
        try {
            object suspendedresources = null;
            if (transaction != null) {
                // 这里是真正做挂起的方法,这里返回的是一个holder
                suspendedresources = dosuspend(transaction);
            }
            // 这里将名称、隔离级别等信息从线程变量中取出并设置对应属性为null到线程变量
            string name = transactionsynchronizationmanager.getcurrenttransactionname();
            transactionsynchronizationmanager.setcurrenttransactionname(null);
            boolean readonly = transactionsynchronizationmanager.iscurrenttransactionreadonly();
            transactionsynchronizationmanager.setcurrenttransactionreadonly(false);
            integer isolationlevel = transactionsynchronizationmanager.getcurrenttransactionisolationlevel();
            transactionsynchronizationmanager.setcurrenttransactionisolationlevel(null);
            boolean wasactive = transactionsynchronizationmanager.isactualtransactionactive();
            transactionsynchronizationmanager.setactualtransactionactive(false);
            // 将事务各个属性与挂起的holder一并封装进suspendedresourcesholder对象中
            return new suspendedresourcesholder(
                suspendedresources, suspendedsynchronizations, name, readonly, isolationlevel, wasactive);
        }
        catch (runtimeexception | error ex) {
            // dosuspend failed - original transaction is still active...
            doresumesynchronization(suspendedsynchronizations);
            throw ex;
        }
    }
    else if (transaction != null) {
        // transaction active but no synchronization active.
        object suspendedresources = dosuspend(transaction);
        return new suspendedresourcesholder(suspendedresources);
    }
    else {
        // neither transaction nor synchronization active.
        return null;
    }
}

 

@override
protected object dosuspend(object transaction) {
    datasourcetransactionobject txobject = (datasourcetransactionobject) transaction;
    // 将transaction中的holder属性设置为空
    txobject.setconnectionholder(null);
    // connnectionholder从线程变量中解绑!
    return transactionsynchronizationmanager.unbindresource(obtaindatasource());
}

 

我们来看看 unbindresource

private static object dounbindresource(object actualkey) {
    // 取得当前线程的线程变量map
    map<object, object> map = resources.get();
    if (map == null) {
        return null;
    }
    // 将key为datasourece的value移除出map,然后将旧的holder返回
    object value = map.remove(actualkey);
    // remove entire threadlocal if empty...
    // 如果此时map为空,直接清除线程变量
    if (map.isempty()) {
        resources.remove();
    }
    // transparently suppress a resourceholder that was marked as void...
    if (value instanceof resourceholder && ((resourceholder) value).isvoid()) {
        value = null;
    }
    if (value != null && logger.istraceenabled()) {
        logger.trace("removed value [" + value + "] for key [" + actualkey + "] from thread [" +
                     thread.currentthread().getname() + "]");
    }
    // 将旧holder返回
    return value;
}

 

可以回头看一下解绑操作的介绍。这里挂起主要干了三件事:

  1. 将transaction中的holder属性设置为空
  2. 解绑(会返回线程中的那个旧的holder出来,从而封装到suspendedresourcesholder对象中)
  3. 将suspendedresourcesholder放入status中,方便后期子事务完成后,恢复外层事务

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网