如何设计网站制作方案,win8风格 网站模板,白酒企业网站源码,如何制作香水 简单大家好#xff0c;我是烤鸭#xff1a; 今天分享的是spring 多线程事务源码分析。
环境#xff1a; spring-jdbc 5.0.4.REALEASE
今天分享一下spring事务的方法#xff0c;这一篇还没涉及到多线程。
简单说一下doBegin的方法#xff1a;
强转获取当前的事务对象#…大家好我是烤鸭 今天分享的是spring 多线程事务源码分析。
环境 spring-jdbc 5.0.4.REALEASE
今天分享一下spring事务的方法这一篇还没涉及到多线程。
简单说一下doBegin的方法
强转获取当前的事务对象看看事务对象是否有连接保持器是否是新的事务对象 或者连接保持器的事务同步性默认是新的事务对象不同步为false。 满足上面的条件为当前连接设置一个新的连接保持器设置当前保持器事务同步性为同步true。 获取事务的之前的事务隔离级别新事务的话就把当前的事务隔离级别设置为当前事务的previousIsolationLevel。 当前事务是否自动提交如果是自动提交事务对象的 mustRestoreAutoCommit 属性设置为true设置自动提交为 false。 ps : mustRestoreAutoCommit这个属性在事务完成之后的 doCleanupAfterCompletion方法中判断如果这个属性为true连接就setAutoCommit 为true。 事务开启后准备正常的事务连接设置事务的只读标识为false。 设置当前事务的当前持有者激活属性为true表示事务在激活中。 设置连接器的超时时间如果超时时间非默认就设置超时时间。 ※※※ 如果是新的事务连接器用ThreadLocal的Map变量,key 是真实去包装后的数据源,value 是当前的连接持有者。将连接和线程绑定 在详细看一下 DataSourceTransactionManager 的doBegin方法 需要传两个参数
/*** This implementation sets the isolation level but ignores the timeout.*///0 TransactionDefinition Override // 0protected void doBegin(Object transaction, TransactionDefinition definition) {//强转成数据源事务对象 1DataSourceTransactionObject txObject (DataSourceTransactionObject) transaction;Connection con null;try {//事务对象是否有连接保持器 2 或者 if (!txObject.hasConnectionHolder() || //连接保持器 是否是同步事务 3txObject.getConnectionHolder().isSynchronizedWithTransaction()) {//获取当前数据源的连接Connection newCon obtainDataSource().getConnection();if (logger.isDebugEnabled()) {logger.debug(Acquired Connection [ newCon ] for JDBC transaction);}//设置 数据源事务对象 的连接持有者ConnectionHolder与当前连接绑定txObject.setConnectionHolder(new ConnectionHolder(newCon), true);}// 设置连接持有者的事务为同步事务txObject.getConnectionHolder().setSynchronizedWithTransaction(true);con txObject.getConnectionHolder().getConnection();// 5 准备为当前连接加事务,获取并设置之前事务的隔离级别Integer previousIsolationLevel DataSourceUtils.prepareConnectionForTransaction(con, definition);txObject.setPreviousIsolationLevel(previousIsolationLevel);// Switch to manual commit if necessary. This is very expensive in some JDBC drivers,// so we dont want to do it unnecessarily (for example if weve explicitly// configured the connection pool to set it already).// 必须时切换手动提交事务。在一些JDBC驱动中是很昂贵的非必须的话不要用。(比如我们已经把连接池设置冲突)// 如果是自动提交,事务对象的必须恢复自动提交为true,连接的自动提交关闭。if (con.getAutoCommit()) {// doCleanupAfterCompletion方法中 事务结束时会判断这个值为true时连接才会自动提交。txObject.setMustRestoreAutoCommit(true);if (logger.isDebugEnabled()) {logger.debug(Switching JDBC Connection [ con ] to manual commit);}con.setAutoCommit(false);}// 6 准备开启事务,设置事务的只读标识为falseprepareTransactionalConnection(con, definition);// 7 设置当前事务被当前持有者 持有txObject.getConnectionHolder().setTransactionActive(true);// **** 8 这个方法就不贴了,就是看当前的definition对象是否有默认超时时间,如果没有就将管理器的超时时间设置到当前连接对象int timeout determineTimeout(definition);if (timeout ! TransactionDefinition.TIMEOUT_DEFAULT) {txObject.getConnectionHolder().setTimeoutInSeconds(timeout);}// Bind the connection holder to the thread.// 9 将连接持有者和线程绑定if (txObject.isNewConnectionHolder()) {// 和线程绑定,key 是真实去包装后的数据源,value 是当前的连接持有者 TransactionSynchronizationManager.bindResource(obtainDataSource(), txObject.getConnectionHolder());}}catch (Throwable ex) {//出异常,如果是新创建的就释放连接,设置当前事务对象连接连接持有者为空if (txObject.isNewConnectionHolder()) {DataSourceUtils.releaseConnection(con, obtainDataSource());txObject.setConnectionHolder(null, false);}throw new CannotCreateTransactionException(Could not open JDBC Connection for transaction, ex);}} 0 TransactionDefinition就是一个关于事务的定义类(常量),其中包含隔离级别,传播类型,只读标识超时时间等。
/*** Interface that defines Spring-compliant transaction properties.* Based on the propagation behavior definitions analogous to EJB CMT attributes.** pNote that isolation level and timeout settings will not get applied unless* an actual new transaction gets started. As only {link #PROPAGATION_REQUIRED},* {link #PROPAGATION_REQUIRES_NEW} and {link #PROPAGATION_NESTED} can cause* that, it usually doesnt make sense to specify those settings in other cases.* Furthermore, be aware that not all transaction managers will support those* advanced features and thus might throw corresponding exceptions when given* non-default values.** pThe {link #isReadOnly() read-only flag} applies to any transaction context,* whether backed by an actual resource transaction or operating non-transactionally* at the resource level. In the latter case, the flag will only apply to managed* resources within the application, such as a Hibernate {code Session}.*/// 接口定义了spring规则的事务属性。基于类似EJBEnterprise Java Bean// CMT(Container-managed transaction) 属性 的依赖传播行为 。// 隔离级别和超时设定只有在真正的新事务开始时才生效。例如PROPAGATION_REQUIRED,// PROPAGATION_REQUIRES_NEW , PROPAGATION_NESTED 是可以的通常不会在其他隔离级别// 指定这些设置。要知道不是所有的事务管理器都支持这些高级特性如果没给默认值有可能// 抛出相应的异常。
public interface TransactionDefinition {/*** Support a current transaction; create a new one if none exists.* Analogous to the EJB transaction attribute of the same name.* pThis is typically the default setting of a transaction definition,* and typically defines a transaction synchronization scope.*/// 支持当前事务;如果不存在就创建一个新的事务。// 类似 相同名字的 EJB 事务 属性。// 这通常是事务定义的默认设置,通常定义事务的同步范围。int PROPAGATION_REQUIRED 0;/*** Support a current transaction; execute non-transactionally if none exists.* Analogous to the EJB transaction attribute of the same name.* pbNOTE:/b For transaction managers with transaction synchronization,* {code PROPAGATION_SUPPORTS} is slightly different from no transaction* at all, as it defines a transaction scope that synchronization might apply to.* As a consequence, the same resources (a JDBC {code Connection}, a* Hibernate {code Session}, etc) will be shared for the entire specified* scope. Note that the exact behavior depends on the actual synchronization* configuration of the transaction manager!* pIn general, use {code PROPAGATION_SUPPORTS} with care! In particular, do* not rely on {code PROPAGATION_REQUIRED} or {code PROPAGATION_REQUIRES_NEW}* iwithin/i a {code PROPAGATION_SUPPORTS} scope (which may lead to* synchronization conflicts at runtime). If such nesting is unavoidable, make sure* to configure your transaction manager appropriately (typically switching to* synchronization on actual transaction).* see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization* see org.springframework.transaction.support.AbstractPlatformTransactionManager#SYNCHRONIZATION_ON_ACTUAL_TRANSACTION*/// 支持当前事务;如果不存在就按无事务的方式进行。// 对于事务同步的事务管理器{code PROPAGATION_SUPPORTS}// 完全不同于非事务,因为可能定义了,同步可能会应用的事务范围// 作为结果,相同的资源(jdbc 连接),Hibernate(Session 会话)等等// 会在指定的全部范围内共享。真正的事务管理器的传播行为取决于真正的同步配置。// // 通常小心使用PROPAGATION_SUPPORTS特别是不要在PROPAGATION_SUPPORTS的范围内可能导致运行时同步冲突// 使用PROPAGATION_REQUIRED和PROPAGATION_REQUIRES_NEW,如果这种嵌套不可避免,确保正确配置事务管理器。通常切换到真正事务上的同步int PROPAGATION_SUPPORTS 1;/*** Support a current transaction; throw an exception if no current transaction* exists. Analogous to the EJB transaction attribute of the same name.* pNote that transaction synchronization within a {code PROPAGATION_MANDATORY}* scope will always be driven by the surrounding transaction.*/// 支持当前事务;如果不存在就按抛出异常。// 类似 相同名字的 EJB 事务 属性。// ps:在 PROPAGATION_MANDATORY的范围中的事务同步总会受周边事务影响。int PROPAGATION_MANDATORY 2;/*** Create a new transaction, suspending the current transaction if one exists.* Analogous to the EJB transaction attribute of the same name.* pbNOTE:/b Actual transaction suspension will not work out-of-the-box* on all transaction managers. This in particular applies to* {link org.springframework.transaction.jta.JtaTransactionManager},* which requires the {code javax.transaction.TransactionManager} to be* made available it to it (which is server-specific in standard Java EE).* pA {code PROPAGATION_REQUIRES_NEW} scope always defines its own* transaction synchronizations. Existing synchronizations will be suspended* and resumed appropriately.* see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager*/// 创建新的事务,如果当前有事务就暂停当前事务。// 类似 相同名字的 EJB 事务 属性。// 真正的事务暂停不会在所有的事务管理器的箱体外工作。// 特别是对于 JtaTransactionManager 需要将 TransactionManagerJ2EE 的服务端指定标准 变得可行。// PROPAGATION_REQUIRES_NEW 范围一直定义自己事务同步。// 已存在的事务会被适当的暂停和恢复。int PROPAGATION_REQUIRES_NEW 3;/*** Do not support a current transaction; rather always execute non-transactionally.* Analogous to the EJB transaction attribute of the same name.* pbNOTE:/b Actual transaction suspension will not work out-of-the-box* on all transaction managers. This in particular applies to* {link org.springframework.transaction.jta.JtaTransactionManager},* which requires the {code javax.transaction.TransactionManager} to be* made available it to it (which is server-specific in standard Java EE).* pNote that transaction synchronization is inot/i available within a* {code PROPAGATION_NOT_SUPPORTED} scope. Existing synchronizations* will be suspended and resumed appropriately.* see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager*/// 以无事务地方式允许,不支持当前的事务。// 类似 相同名字的 EJB 事务 属性。// 真正的事务暂停不会在所有的事务管理器的箱体外工作。// 特别是对于 JtaTransactionManager 需要将 TransactionManagerJ2EE 的服务端指定标准 变得可行。// PROPAGATION_REQUIRES_NEW 范围一直定义自己事务同步。// 已存在的事务会被适当的暂停和恢复。int PROPAGATION_NOT_SUPPORTED 4;/*** Do not support a current transaction; throw an exception if a current transaction* exists. Analogous to the EJB transaction attribute of the same name.* pNote that transaction synchronization is inot/i available within a* {code PROPAGATION_NEVER} scope.*/// 不支持当前事务;如果当前事务存在就按抛出异常。// 类似 相同名字的 EJB 事务 属性。// 事务同步在 PROPAGATION_NEVER 范围内是别可用的。int PROPAGATION_NEVER 5;/*** Execute within a nested transaction if a current transaction exists,* behave like {link #PROPAGATION_REQUIRED} else. There is no analogous* feature in EJB.* pbNOTE:/b Actual creation of a nested transaction will only work on* specific transaction managers. Out of the box, this only applies to the JDBC* {link org.springframework.jdbc.datasource.DataSourceTransactionManager}* when working on a JDBC 3.0 driver. Some JTA providers might support* nested transactions as well.* see org.springframework.jdbc.datasource.DataSourceTransactionManager*/// 如果当前事务存在,在嵌套事务中执行,类似 PROPAGATION_REQUIRED 的行为。// 和 EJB 中没有类似的。// 真正的嵌套事务的创建只会在指定的事务管理器中工作。// 当使用JDBC 3.0驱动时,在箱体外,唯一可用的是JDBC(DataSourceTransactionManager) 。// 一些 JTA 提供者也许也支持嵌套事务。int PROPAGATION_NESTED 6;/*** Use the default isolation level of the underlying datastore.* All other levels correspond to the JDBC isolation levels.* see java.sql.Connection*/// 在当前的数据仓库下,使用默认的隔离级别。// 所有其他的级别和 JDBC 级别保持一致。int ISOLATION_DEFAULT -1;/*** Indicates that dirty reads, non-repeatable reads and phantom reads* can occur.* pThis level allows a row changed by one transaction to be read by another* transaction before any changes in that row have been committed (a dirty read).* If any of the changes are rolled back, the second transaction will have* retrieved an invalid row.* see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED*/// 暗示脏读,不可重复读和幻读有可能发生。// 这个级别允许事务改变一行数据,在这一行数据修改而提交之前,被另一个事务读到了脏读。// 如果任何改变回滚,第二个事务会恢复有一个非法的行数据。int ISOLATION_READ_UNCOMMITTED Connection.TRANSACTION_READ_UNCOMMITTED;/*** Indicates that dirty reads are prevented; non-repeatable reads and* phantom reads can occur.* pThis 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;/*** Indicates that dirty reads and non-repeatable reads are prevented;* phantom reads can occur.* pThis level prohibits a transaction from reading a row with uncommitted changes* in it, and it also prohibits the situation where one transaction reads a row,* a second transaction alters the row, and the first transaction re-reads the row,* getting different values the second time (a non-repeatable read).* see java.sql.Connection#TRANSACTION_REPEATABLE_READ*/// 暗示脏读和不可重复读会被禁止,幻读可能发生。// 这个级别会阻止事务读取到未提交的改变,// 也会阻止如果有事务读取一行,第二个事务改变这行,第一个事务再读取这行,// 第二次会有不一样的值(不可重复读)// 详见 TRANSACTION_REPEATABLE_READint ISOLATION_REPEATABLE_READ Connection.TRANSACTION_REPEATABLE_READ;/*** Indicates that dirty reads, non-repeatable reads and phantom reads* are prevented.* pThis level includes the prohibitions in {link #ISOLATION_REPEATABLE_READ}* and further prohibits the situation where one transaction reads all rows that* satisfy a {code WHERE} condition, a second transaction inserts a row* that satisfies that {code WHERE} condition, and the first transaction* re-reads for the same condition, retrieving the additional phantom row* in the second read.* see java.sql.Connection#TRANSACTION_SERIALIZABLE*/// 暗示脏读和不可重复读和幻读会被禁止。// 这个级别包含的禁止情况ISOLATION_REPEATABLE_READ和更多的情况,// 类似一个事务读取所有的行where条件的情况,第二个事务根据where条件// 插入一行,第一个事务再重新执行刚才的sql,第二次读取就会读到额外的幻影行。int ISOLATION_SERIALIZABLE Connection.TRANSACTION_SERIALIZABLE;/*** Use the default timeout of the underlying transaction system,* or none if timeouts are not supported.*/// 再当前的事务系统使用默认的超时时间如果不支持超时时间就没有。int TIMEOUT_DEFAULT -1;/*** Return the propagation behavior.* pMust return one of the {code PROPAGATION_XXX} constants* defined on {link TransactionDefinition this interface}.* return the propagation behavior* see #PROPAGATION_REQUIRED* see org.springframework.transaction.support.TransactionSynchronizationManager#isActualTransactionActive()*/// 返回传播行为。// 必须返回 在 TransactionDefinition 接口中定义的 PROPAGATION_XXX 这样的常量int getPropagationBehavior();/*** Return the isolation level.* pMust return one of the {code ISOLATION_XXX} constants defined on* {link TransactionDefinition this interface}. Those constants are designed* to match the values of the same constants on {link java.sql.Connection}.* pExclusively designed for use with {link #PROPAGATION_REQUIRED} or* {link #PROPAGATION_REQUIRES_NEW} since it only applies to newly started* transactions. Consider switching the validateExistingTransactions flag to* true on your transaction manager if youd like isolation level declarations* to get rejected when participating in an existing transaction with a different* isolation level.* pNote that a transaction manager that does not support custom isolation levels* will throw an exception when given any other level than {link #ISOLATION_DEFAULT}.* return the isolation level* see #ISOLATION_DEFAULT* see org.springframework.transaction.support.AbstractPlatformTransactionManager#setValidateExistingTransaction*/// 返回隔离级别。// 必须返回 在 TransactionDefinition 接口中定义的 ISOLATION_XXX 这样的常量// 这些常量被设计成和 link java.sql.Connection 中的常量一致。// PROPAGATION_REQUIRED 和 PROPAGATION_REQUIRES_NEW 被专门设计应用于新开启的事务。// 记得将事务管理器的validateExistingTransactions的标识切换至true,// 如果你的隔离级别声明被拒绝当有不同的隔离界别参加已存在事务时。// 事务管理器不支持普通的隔离级别,如果有默认隔离级别之外的,会抛异常。 int getIsolationLevel();/*** Return the transaction timeout.* pMust return a number of seconds, or {link #TIMEOUT_DEFAULT}.* pExclusively designed for use with {link #PROPAGATION_REQUIRED} or* {link #PROPAGATION_REQUIRES_NEW} since it only applies to newly started* transactions.* pNote that a transaction manager that does not support timeouts will throw* an exception when given any other timeout than {link #TIMEOUT_DEFAULT}.* return the transaction timeout*/// 返回事务的超时时间。// 必须返回秒数。或者 默认超时时间// PROPAGATION_REQUIRED 和 PROPAGATION_REQUIRES_NEW 被专门设计应用于新开启的事务。// 如果超时时间用在 不支持超时时间的事务管理器 会抛出异常。int getTimeout();/*** Return whether to optimize as a read-only transaction.* pThe read-only flag applies to any transaction context, whether backed* by an actual resource transaction ({link #PROPAGATION_REQUIRED}/* {link #PROPAGATION_REQUIRES_NEW}) or operating non-transactionally at* the resource level ({link #PROPAGATION_SUPPORTS}). In the latter case,* the flag will only apply to managed resources within the application,* such as a Hibernate {code Session}.* pThis just serves as a hint for the actual transaction subsystem;* it will inot necessarily/i cause failure of write access attempts.* A transaction manager which cannot interpret the read-only hint will* inot/i throw an exception when asked for a read-only transaction.* return {code true} if the transaction is to be optimized as read-only* see org.springframework.transaction.support.TransactionSynchronization#beforeCommit(boolean)* see org.springframework.transaction.support.TransactionSynchronizationManager#isCurrentTransactionReadOnly()*/// 返回事务是否只读。// 只读标识应用在事务作用域,无论是被真正的资源事务支持(如 PROPAGATION_REQUIRED/PROPAGATION_REQUIRES_NEW)// 或者在资源级别(PROPAGATION_SUPPORTS) 操作无事务,在后者的例子,例如 Hibernate(会话)。// 这只是对于真正的事务子系统的一个提示,不会必然导致写尝试进程的失败。// 事务管理器不会解释只读的提示,当被一个只读事务调用的时候不会抛出异常。// boolean isReadOnly();/*** Return the name of this transaction. Can be {code null}.* pThis will be used as the transaction name to be shown in a* transaction monitor, if applicable (for example, WebLogics).* pIn case of Springs declarative transactions, the exposed name will be* the {code fully-qualified class name . method name} (by default).* return the name of this transaction* see org.springframework.transaction.interceptor.TransactionAspectSupport* see org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionName()*/// 返回事务的名称,被用于事务监视中,可用的(如WebLogic)// 如果是spring的声明事务,暴露的名称就是全类名.方法名NullableString getName();} 1 transaction强转成DataSourceTransactionObject先去看一下 DataSourceTransactionObject 是什么类。 DataSourceTransactionObject是DataSourceTransactionManager的内部类。 DataSourceTransactionObject类说明
/*** DataSource transaction object, representing a ConnectionHolder.* Used as transaction object by DataSourceTransactionManager.*/// 数据源事务的对象代表连接保持器作为事务对象被DataSourceTransactionManager调用。 2: hasConnectionHolder()是JdbcTransactionObjectSupport的方法。判断该类的connectionHandle 是否为空。 JdbcTransactionObjectSupport 是什么鬼看一下抽象类 实现了 SavepointManager 和 SmartTransactionObject。
/*** Convenient base class for JDBC-aware transaction objects. Can contain a* {link ConnectionHolder} with a JDBC {code Connection}, and implements the* {link SavepointManager} interface based on that {code ConnectionHolder}.** pAllows for programmatic management of JDBC {link java.sql.Savepoint Savepoints}.* Springs {link org.springframework.transaction.support.DefaultTransactionStatus}* automatically delegates to this, as it autodetects transaction objects which* implement the {link SavepointManager} interface.*///便于jdbc的事务对象继承的基类。能够获得jdbc连接状态实现了SavepointManager接口。//允许对jdbc程序化管理spring的默认事务状态自动委托给这个自动检测事务对象。public abstract class JdbcTransactionObjectSupport implements SavepointManager, SmartTransactionObject
再看SavepointManager 。
仨方法 创建/回滚到/释放 保存点。
/*** Interface that specifies an API to programmatically manage transaction* savepoints in a generic fashion. Extended by TransactionStatus to* expose savepoint management functionality for a specific transaction.** pNote that savepoints can only work within an active transaction.* Just use this programmatic savepoint handling for advanced needs;* else, a subtransaction with PROPAGATION_NESTED is preferable.** pThis interface is inspired by JDBC 3.0s Savepoint mechanism* but is independent from any specific persistence technology.**/// 是一个接口指定的API能够在通常情况下程序化地管理事务保存点。// 对于指定的事务,扩展的事务状态展示了功能化的保存点管理。// ps:保存点出现在活跃事务中在高级需求中使用程序化保存点// 当用到 PROPAGATION_NESTED 级别的子事务的时候更好。// 这个接口受启发于jdbc3.0保存点原理但是独立于任何特定的持久化技术。public interface SavepointManager {/*** Create a new savepoint. You can roll back to a specific savepoint* via {code rollbackToSavepoint}, and explicitly release a savepoint* that you dont need anymore via {code releaseSavepoint}.* pNote that most transaction managers will automatically release* savepoints at transaction completion.* return a savepoint object, to be passed into* {link #rollbackToSavepoint} or {link #releaseSavepoint}* throws NestedTransactionNotSupportedException if the underlying* transaction does not support savepoints* throws TransactionException if the savepoint could not be created,* for example because the transaction is not in an appropriate state* see java.sql.Connection#setSavepoint*///创建保存点能够通过rollbackToSavepoint回滚到特定的保存点可以在//不需要的时候通过releaseSavepoint方法释放保存点。大部分事务管理器会在事务//完成的时候释放保存点。Object createSavepoint() throws TransactionException;/*** Roll back to the given savepoint.* pThe savepoint will inot/i be automatically released afterwards.* You may explicitly call {link #releaseSavepoint(Object)} or rely on* automatic release on transaction completion.* param savepoint the savepoint to roll back to* throws NestedTransactionNotSupportedException if the underlying* transaction does not support savepoints* throws TransactionException if the rollback failed* see java.sql.Connection#rollback(java.sql.Savepoint)*///回滚到给出的保存点。//保存点最后不会自动释放。可以调用释放方法或者等事务结束自动关闭。void rollbackToSavepoint(Object savepoint) throws TransactionException;/*** Explicitly release the given savepoint.* pNote that most transaction managers will automatically release* savepoints on transaction completion.* pImplementations should fail as silently as possible if proper* resource cleanup will eventually happen at transaction completion.* param savepoint the savepoint to release* throws NestedTransactionNotSupportedException if the underlying* transaction does not support savepoints* throws TransactionException if the release failed* see java.sql.Connection#releaseSavepoint*///释放保存点//大部分事务管理器会在事务完成的时候释放保存点。//如果正确的资源清理在事务结束时发生这个方法实现可能失败。void releaseSavepoint(Object savepoint) throws TransactionException;} 一句话概括一下就是嵌套事务中有个存档当我们设置了存档出异常的事务可以回滚到存档不至于全部回滚。 嵌套事务(事务隔壁级别是PROPAGATION_NESTED) 食用更佳。 SmartTransactionObject public interface SmartTransactionObject extends Flushable {/*** Interface to be implemented by transaction objects that are able to* return an internal rollback-only marker, typically from a another* transaction that has participated and marked it as rollback-only.** pAutodetected by DefaultTransactionStatus, to always return a* current rollbackOnly flag even if not resulting from the current* TransactionStatus.*///被用于事务对象实现的接口能够返回一个内部的仅回滚标记代表有另一个事务参与并标记为仅回滚。/*** Return whether the transaction is internally marked as rollback-only.* Can, for example, check the JTA UserTransaction.* see javax.transaction.UserTransaction#getStatus* see javax.transaction.Status#STATUS_MARKED_ROLLBACK*///返回是否有另一个事务参与。boolean isRollbackOnly();/*** Flush the underlying sessions to the datastore, if applicable:* for example, all affected Hibernate/JPA sessions.*///如果适用的话刷新潜在的会话回数据源比如所有的影响的Hibernate/JPA会话。清理缓存区的资源Overridevoid flush();} 3: isSynchronizedWithTransaction() 是ResourceHolderSupport 类中的方法 /*** Convenient base class for resource holders.** pFeatures rollback-only support for participating transactions.* Can expire after a certain number of seconds or milliseconds* in order to determine a transactional timeout.*///资源持有者的实用的基类//为了让事务能够在一定时间内过期,对于参与进来的事务提供仅回滚的支持public abstract class ResourceHolderSupport implements ResourceHolder/*** Return whether the resource is synchronized with a transaction.*/// 当前资源持有者是否与事务同步public boolean isSynchronizedWithTransaction() {return this.synchronizedWithTransaction;}
} 再看实现的接口 ResourceHolder /*** Generic interface to be implemented by resource holders.* Allows Springs transaction infrastructure to introspect* and reset the holder when necessary.**///通用接口用来被资源持有者实现。允许spring的事务架构必要时直接看到和重置持有者。public interface ResourceHolder {/*** Reset the transactional state of this holder.*/// 重置持有者的事务状态。void reset();/*** Notify this holder that it has been unbound from transaction synchronization.*/// 通知持有者已经和事务同步解绑了void unbound();/*** Determine whether this holder is considered as void,* i.e. as a leftover from a previous thread.*/// 决定持有者是否是无效的boolean isVoid();} 5 数据源工具类,这里用到的是prepareConnectionForTransaction,具体看下面的方法注释。
/*** Helper class that provides static methods for obtaining JDBC Connections from* a {link javax.sql.DataSource}. Includes special support for Spring-managed* transactional Connections, e.g. managed by {link DataSourceTransactionManager}* or {link org.springframework.transaction.jta.JtaTransactionManager}.** pUsed internally by Springs {link org.springframework.jdbc.core.JdbcTemplate},* Springs JDBC operation objects and the JDBC {link DataSourceTransactionManager}.* Can also be used directly in application code.*///工具类提供静态方法从数据源中获取jdbc连接包括对spring管理的事务拦截的特别支撑//在被spring的JdbcTemplatespring的jdbc操作对象和jdbc事务管理器使用。也能被直接用在应用代码中。
public abstract class DataSourceUtils {/*** Order value for TransactionSynchronization objects that clean up JDBC Connections.*/// TransactionSynchronization清理jdbc连接的对象的顺序值public static final int CONNECTION_SYNCHRONIZATION_ORDER 1000;private static final Log logger LogFactory.getLog(DataSourceUtils.class);/*** Obtain a Connection from the given DataSource. Translates SQLExceptions into* the Spring hierarchy of unchecked generic data access exceptions, simplifying* calling code and making any exception that is thrown more meaningful.* pIs aware of a corresponding Connection bound to the current thread, for example* when using {link DataSourceTransactionManager}. Will bind a Connection to the* thread if transaction synchronization is active, e.g. when running within a* {link org.springframework.transaction.jta.JtaTransactionManager JTA} transaction).* param dataSource the DataSource to obtain Connections from* return a JDBC Connection from the given DataSource* throws org.springframework.jdbc.CannotGetJdbcConnectionException* if the attempt to get a Connection failed* see #releaseConnection*///通过数据源获取连接。将sql异常抛给spring的层级中未检查的普通数据简化调用代码让任何异常都清晰//要知道比如使用连接池数据管理器一致的连接是和当前线程绑定。//举例子如果JTA事务中同步事务被激活连接和线程绑定。public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {try {return doGetConnection(dataSource);}catch (SQLException ex) {throw new CannotGetJdbcConnectionException(Failed to obtain JDBC Connection, ex);}catch (IllegalStateException ex) {throw new CannotGetJdbcConnectionException(Failed to obtain JDBC Connection: ex.getMessage());}}/*** Actually obtain a JDBC Connection from the given DataSource.* Same as {link #getConnection}, but throwing the original SQLException.* pIs aware of a corresponding Connection bound to the current thread, for example* when using {link DataSourceTransactionManager}. Will bind a Connection to the thread* if transaction synchronization is active (e.g. if in a JTA transaction).* pDirectly accessed by {link TransactionAwareDataSourceProxy}.* param dataSource the DataSource to obtain Connections from* return a JDBC Connection from the given DataSource* throws SQLException if thrown by JDBC methods* see #doReleaseConnection*///获取当前数据源的真正连接//和getConnection方法一样但抛出的是原始的sql异常。//要知道比如使用连接池数据管理器一致的连接是和当前线程绑定。//举例子如果JTA事务中同步事务被激活连接和线程绑定。public static Connection doGetConnection(DataSource dataSource) throws SQLException {Assert.notNull(dataSource, No DataSource specified);ConnectionHolder conHolder (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);if (conHolder ! null (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {conHolder.requested();if (!conHolder.hasConnection()) {logger.debug(Fetching resumed JDBC Connection from DataSource);conHolder.setConnection(fetchConnection(dataSource));}return conHolder.getConnection();}// Else we either got no holder or an empty thread-bound holder here.logger.debug(Fetching JDBC Connection from DataSource);Connection con fetchConnection(dataSource);if (TransactionSynchronizationManager.isSynchronizationActive()) {logger.debug(Registering transaction synchronization for JDBC Connection);// Use same Connection for further JDBC actions within the transaction.// Thread-bound object will get removed by synchronization at transaction completion.ConnectionHolder holderToUse conHolder;if (holderToUse null) {holderToUse new ConnectionHolder(con);}else {holderToUse.setConnection(con);}holderToUse.requested();TransactionSynchronizationManager.registerSynchronization(new ConnectionSynchronization(holderToUse, dataSource));holderToUse.setSynchronizedWithTransaction(true);if (holderToUse ! conHolder) {TransactionSynchronizationManager.bindResource(dataSource, holderToUse);}}return con;}/*** Actually fetch a {link Connection} from the given {link DataSource},* defensively turning an unexpected {code null} return value from* {link DataSource#getConnection()} into an {link IllegalStateException}.* param dataSource the DataSource to obtain Connections from* return a JDBC Connection from the given DataSource (never {code null})* throws SQLException if thrown by JDBC methods* throws IllegalStateException if the DataSource returned a null value* see DataSource#getConnection()*///从数据源获取连接如果返回空抛异常。private static Connection fetchConnection(DataSource dataSource) throws SQLException {Connection con dataSource.getConnection();if (con null) {throw new IllegalStateException(DataSource returned null from getConnection(): dataSource);}return con;}/*** Prepare the given Connection with the given transaction semantics.* param con the Connection to prepare* param definition the transaction definition to apply* return the previous isolation level, if any* throws SQLException if thrown by JDBC methods* see #resetConnectionAfterTransaction*///准备给传入的连接加事务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 its a connection timeout that would otherwise get lost: e.g. from JDBC 4.0throw ex;}exToCheck exToCheck.getCause();}// read-only not supported SQLException - ignore, its just a hint anyway//只读不支持sql异常忽略这个只是一个提示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;}/*** Reset the given Connection after a transaction,* regarding read-only flag and isolation level.* param con the Connection to reset* param previousIsolationLevel the isolation level to restore, if any* see #prepareConnectionForTransaction*///重置给定连接的事务包括只读标识和隔离级别public static void resetConnectionAfterTransaction(Connection con, Nullable Integer previousIsolationLevel) {Assert.notNull(con, No Connection specified);try {// Reset transaction isolation to previous value, if changed for the transaction.// 如果事务更改则将事务隔离重置为前值if (previousIsolationLevel ! null) {if (logger.isDebugEnabled()) {logger.debug(Resetting isolation level of JDBC Connection [ con ] to previousIsolationLevel);}con.setTransactionIsolation(previousIsolationLevel);}// Reset read-only flag.if (con.isReadOnly()) {if (logger.isDebugEnabled()) {logger.debug(Resetting read-only flag of JDBC Connection [ con ]);}con.setReadOnly(false);}}catch (Throwable ex) {logger.debug(Could not reset JDBC Connection after transaction, ex);}}/*** Determine whether the given JDBC Connection is transactional, that is,* bound to the current thread by Springs transaction facilities.* param con the Connection to check* param dataSource the DataSource that the Connection was obtained from* (may be {code null})* return whether the Connection is transactional*///决定给定的jdbc连接是否支持事务spring事务设置的是和当前线程绑定。public static boolean isConnectionTransactional(Connection con, Nullable DataSource dataSource) {if (dataSource null) {return false;}ConnectionHolder conHolder (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);return (conHolder ! null connectionEquals(conHolder, con));}/*** Apply the current transaction timeout, if any,* to the given JDBC Statement object.* param stmt the JDBC Statement object* param dataSource the DataSource that the Connection was obtained from* throws SQLException if thrown by JDBC methods* see java.sql.Statement#setQueryTimeout*///如果有的话对于jdbc陈述对象的应用当前事务的声明的超时时间。public static void applyTransactionTimeout(Statement stmt, Nullable DataSource dataSource) throws SQLException {applyTimeout(stmt, dataSource, -1);}/*** Apply the specified timeout - overridden by the current transaction timeout,* if any - to the given JDBC Statement object.* param stmt the JDBC Statement object* param dataSource the DataSource that the Connection was obtained from* param timeout the timeout to apply (or 0 for no timeout outside of a transaction)* throws SQLException if thrown by JDBC methods* see java.sql.Statement#setQueryTimeout*///如果有的话对于jdbc陈述对象的接受被当前事务所覆盖的特定的超时时间。public static void applyTimeout(Statement stmt, Nullable DataSource dataSource, int timeout) throws SQLException {Assert.notNull(stmt, No Statement specified);ConnectionHolder holder null;if (dataSource ! null) {holder (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);}if (holder ! null holder.hasTimeout()) {// Remaining transaction timeout overrides specified value.stmt.setQueryTimeout(holder.getTimeToLiveInSeconds());}else if (timeout 0) {// No current transaction timeout - apply specified value.stmt.setQueryTimeout(timeout);}}/*** Close the given Connection, obtained from the given DataSource,* if it is not managed externally (that is, not bound to the thread).* param con the Connection to close if necessary* (if this is {code null}, the call will be ignored)* param dataSource the DataSource that the Connection was obtained from* (may be {code null})* see #getConnection*///如果不是外部管理非线程绑定关闭从数据源获取到的连接。public static void releaseConnection(Nullable Connection con, Nullable DataSource dataSource) {try {doReleaseConnection(con, dataSource);}catch (SQLException ex) {logger.debug(Could not close JDBC Connection, ex);}catch (Throwable ex) {logger.debug(Unexpected exception on closing JDBC Connection, ex);}}/*** Actually close the given Connection, obtained from the given DataSource.* Same as {link #releaseConnection}, but throwing the original SQLException.* pDirectly accessed by {link TransactionAwareDataSourceProxy}.* param con the Connection to close if necessary* (if this is {code null}, the call will be ignored)* param dataSource the DataSource that the Connection was obtained from* (may be {code null})* throws SQLException if thrown by JDBC methods* see #doGetConnection*///真实关闭从数据源获取到的连接和释放连接方法一样但是抛出一个sql异常public static void doReleaseConnection(Nullable Connection con, Nullable DataSource dataSource) throws SQLException {if (con null) {return;}if (dataSource ! null) {ConnectionHolder conHolder (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);if (conHolder ! null connectionEquals(conHolder, con)) {// Its the transactional Connection: Dont close it.conHolder.released();return;}}logger.debug(Returning JDBC Connection to DataSource);doCloseConnection(con, dataSource);}/*** Close the Connection, unless a {link SmartDataSource} doesnt want us to.* param con the Connection to close if necessary* param dataSource the DataSource that the Connection was obtained from* throws SQLException if thrown by JDBC methods* see Connection#close()* see SmartDataSource#shouldClose(Connection)*///关闭异常除非SmartDataSource 不想让我们关闭。public static void doCloseConnection(Connection con, Nullable DataSource dataSource) throws SQLException {if (!(dataSource instanceof SmartDataSource) || ((SmartDataSource) dataSource).shouldClose(con)) {con.close();}}/*** Determine whether the given two Connections are equal, asking the target* Connection in case of a proxy. Used to detect equality even if the* user passed in a raw target Connection while the held one is a proxy.* param conHolder the ConnectionHolder for the held Connection (potentially a proxy)* param passedInCon the Connection passed-in by the user* (potentially a target Connection without proxy)* return whether the given Connections are equal* see #getTargetConnection*///比较两个连接是否相同询问目标连接是否是代理如果用户在原目标连接传递//当保持其中一个目标连接是代理的时候检测是否相同private static boolean connectionEquals(ConnectionHolder conHolder, Connection passedInCon) {if (!conHolder.hasConnection()) {return false;}Connection heldCon conHolder.getConnection();// Explicitly check for identity too: for Connection handles that do not implement// equals properly, such as the ones Commons DBCP exposes).return (heldCon passedInCon || heldCon.equals(passedInCon) ||getTargetConnection(heldCon).equals(passedInCon));}/*** Return the innermost target Connection of the given Connection. If the given* Connection is a proxy, it will be unwrapped until a non-proxy Connection is* found. Otherwise, the passed-in Connection will be returned as-is.* param con the Connection proxy to unwrap* return the innermost target Connection, or the passed-in one if no proxy* see ConnectionProxy#getTargetConnection()*///返回最深层的目标连接如果连接是代理的将会去包装直到非代理连接。//否则,传递的连接将会被返回。public static Connection getTargetConnection(Connection con) {Connection conToUse con;while (conToUse instanceof ConnectionProxy) {conToUse ((ConnectionProxy) conToUse).getTargetConnection();}return conToUse;}/*** Determine the connection synchronization order to use for the given* DataSource. Decreased for every level of nesting that a DataSource* has, checked through the level of DelegatingDataSource nesting.* param dataSource the DataSource to check* return the connection synchronization order to use* see #CONNECTION_SYNCHRONIZATION_ORDER*///决定连接同步为了使用给定的数据源。减少数据源的嵌套层级通过代理数据源的嵌套//检测层级private static int getConnectionSynchronizationOrder(DataSource dataSource) {int order CONNECTION_SYNCHRONIZATION_ORDER;DataSource currDs dataSource;while (currDs instanceof DelegatingDataSource) {order--;currDs ((DelegatingDataSource) currDs).getTargetDataSource();}return order;}/*** Callback for resource cleanup at the end of a non-native JDBC transaction* (e.g. when participating in a JtaTransactionManager transaction).* see org.springframework.transaction.jta.JtaTransactionManager*///在非本地jdbc事务结束时资源清理的回调private static class ConnectionSynchronization extends TransactionSynchronizationAdapter {private final ConnectionHolder connectionHolder;private final DataSource dataSource;private int order;private boolean holderActive true;public ConnectionSynchronization(ConnectionHolder connectionHolder, DataSource dataSource) {this.connectionHolder connectionHolder;this.dataSource dataSource;this.order getConnectionSynchronizationOrder(dataSource);}Overridepublic int getOrder() {return this.order;}Overridepublic void suspend() {if (this.holderActive) {TransactionSynchronizationManager.unbindResource(this.dataSource);if (this.connectionHolder.hasConnection() !this.connectionHolder.isOpen()) {// Release Connection on suspend if the application doesnt keep// a handle to it anymore. We will fetch a fresh Connection if the// application accesses the ConnectionHolder again after resume,// assuming that it will participate in the same transaction.//如果应用不再保持连接暂停释放资源。再应用再次获取连接持有者后会再次获取新的连接//假设仍会参与到相同的事务中releaseConnection(this.connectionHolder.getConnection(), this.dataSource);this.connectionHolder.setConnection(null);}}}Overridepublic void resume() {if (this.holderActive) {TransactionSynchronizationManager.bindResource(this.dataSource, this.connectionHolder);}}Overridepublic void beforeCompletion() {// Release Connection early if the holder is not open anymore// (that is, not used by another resource like a Hibernate Session// that has its own cleanup via transaction synchronization),// to avoid issues with strict JTA implementations that expect// the close call before transaction completion.// 如果连接不再开放早早释放连接不像Hibernate的另一个资源在事务同步的时候有会// 自己清理避免在事务结束前JTA实现的问题。if (!this.connectionHolder.isOpen()) {TransactionSynchronizationManager.unbindResource(this.dataSource);this.holderActive false;if (this.connectionHolder.hasConnection()) {releaseConnection(this.connectionHolder.getConnection(), this.dataSource);}}}Overridepublic void afterCompletion(int status) {// If we havent closed the Connection in beforeCompletion,// close it now. The holder might have been used for other// cleanup in the meantime, for example by a Hibernate Session.// 如果在完成前不关闭连接现在关闭。这个保持可能同时已经被另一个清理所使用比如Hibernate会话。if (this.holderActive) {// The thread-bound ConnectionHolder might not be available anymore,// since afterCompletion might get called from a different thread.// 这个线程绑定的连接持有者也许不再可用自从完成之后可能会不同的线程回调。TransactionSynchronizationManager.unbindResourceIfPossible(this.dataSource);this.holderActive false;if (this.connectionHolder.hasConnection()) {releaseConnection(this.connectionHolder.getConnection(), this.dataSource);// Reset the ConnectionHolder: It might remain bound to the thread.// 重置连接保持器也许能够和线程绑定。this.connectionHolder.setConnection(null);}}this.connectionHolder.reset();}}} 6 : prepareTransactionalConnection 方法
/*** Prepare the transactional {code Connection} right after transaction begin.* pThe default implementation executes a SET TRANSACTION READ ONLY statement* if the {link #setEnforceReadOnly enforceReadOnly} flag is set to {code true}* and the transaction definition indicates a read-only transaction.* pThe SET TRANSACTION READ ONLY is understood by Oracle, MySQL and Postgres* and may work with other databases as well. If youd like to adapt this treatment,* override this method accordingly.* param con the transactional JDBC Connection* param definition the current transaction definition* throws SQLException if thrown by JDBC API* since 4.3.7* see #setEnforceReadOnly*/// 事务开启后准备正常的事务连接。// 如果 enforceReadOnly 标识是true、事务定义标示一个只读事务默认的实现执行了设置事务为只读 语句。// 设置事务只读是oracle,mysql和postgres和其他数据库的理解。 如果你想适应这个方式,按照一致规则重写这个方法。protected void prepareTransactionalConnection(Connection con, TransactionDefinition definition)throws SQLException {if (isEnforceReadOnly() definition.isReadOnly()) {Statement stmt con.createStatement();try {stmt.executeUpdate(SET TRANSACTION READ ONLY);}finally {stmt.close();}}} 7: ConnectionHolder
/*** Connection holder, wrapping a JDBC Connection.* {link DataSourceTransactionManager} binds instances of this class* to the thread, for a specific DataSource.** pInherits rollback-only support for nested JDBC transactions* and reference count functionality from the base class.** pNote: This is an SPI class, not intended to be used by applications.*///连接持有者是对jdbc连接的包装。事务管理器是和当前类实例和线程绑定的目的是指定连接池。//对于嵌套的hdbc事务从基类继承了仅回滚的支持和功能上的引用计数。//这是一个service provider interface class不被任何应用使用。public class ConnectionHolder extends ResourceHolderSupport {public static final String SAVEPOINT_NAME_PREFIX SAVEPOINT_;Nullableprivate ConnectionHandle connectionHandle;Nullableprivate Connection currentConnection;private boolean transactionActive false;Nullableprivate Boolean savepointsSupported;private int savepointCounter 0;/*** Create a new ConnectionHolder for the given ConnectionHandle.* param connectionHandle the ConnectionHandle to hold*///创建连接持有者public ConnectionHolder(ConnectionHandle connectionHandle) {Assert.notNull(connectionHandle, ConnectionHandle must not be null);this.connectionHandle connectionHandle;}/*** Create a new ConnectionHolder for the given JDBC Connection,* wrapping it with a {link SimpleConnectionHandle},* assuming that there is no ongoing transaction.* param connection the JDBC Connection to hold* see SimpleConnectionHandle* see #ConnectionHolder(java.sql.Connection, boolean)*/public ConnectionHolder(Connection connection) {this.connectionHandle new SimpleConnectionHandle(connection);}/*** Create a new ConnectionHolder for the given JDBC Connection,* wrapping it with a {link SimpleConnectionHandle}.* param connection the JDBC Connection to hold* param transactionActive whether the given Connection is involved* in an ongoing transaction* see SimpleConnectionHandle*/public ConnectionHolder(Connection connection, boolean transactionActive) {this(connection);this.transactionActive transactionActive;}/*** Return the ConnectionHandle held by this ConnectionHolder.*/Nullablepublic ConnectionHandle getConnectionHandle() {return this.connectionHandle;}/*** Return whether this holder currently has a Connection.*/protected boolean hasConnection() {return (this.connectionHandle ! null);}/*** Set whether this holder represents an active, JDBC-managed transaction.* see DataSourceTransactionManager*/protected void setTransactionActive(boolean transactionActive) {this.transactionActive transactionActive;}/*** Return whether this holder represents an active, JDBC-managed transaction.*///返回当前持有者是否代表了活动的jdbc管理的事务。protected boolean isTransactionActive() {return this.transactionActive;}/*** Override the existing Connection handle with the given Connection.* Reset the handle if given {code null}.* pUsed for releasing the Connection on suspend (with a {code null}* argument) and setting a fresh Connection on resume.*/protected void setConnection(Nullable Connection connection) {if (this.currentConnection ! null) {if (this.connectionHandle ! null) {this.connectionHandle.releaseConnection(this.currentConnection);}this.currentConnection null;}if (connection ! null) {this.connectionHandle new SimpleConnectionHandle(connection);}else {this.connectionHandle null;}}/*** Return the current Connection held by this ConnectionHolder.* pThis will be the same Connection until {code released}* gets called on the ConnectionHolder, which will reset the* held Connection, fetching a new Connection on demand.* see ConnectionHandle#getConnection()* see #released()*/public Connection getConnection() {Assert.notNull(this.connectionHandle, Active Connection is required);if (this.currentConnection null) {this.currentConnection this.connectionHandle.getConnection();}return this.currentConnection;}/*** Return whether JDBC 3.0 Savepoints are supported.* Caches the flag for the lifetime of this ConnectionHolder.* throws SQLException if thrown by the JDBC driver*/public boolean supportsSavepoints() throws SQLException {if (this.savepointsSupported null) {this.savepointsSupported getConnection().getMetaData().supportsSavepoints();}return this.savepointsSupported;}/*** Create a new JDBC 3.0 Savepoint for the current Connection,* using generated savepoint names that are unique for the Connection.* return the new Savepoint* throws SQLException if thrown by the JDBC driver*/public Savepoint createSavepoint() throws SQLException {this.savepointCounter;return getConnection().setSavepoint(SAVEPOINT_NAME_PREFIX this.savepointCounter);}/*** Releases the current Connection held by this ConnectionHolder.* pThis is necessary for ConnectionHandles that expect Connection borrowing,* where each returned Connection is only temporarily leased and needs to be* returned once the data operation is done, to make the Connection available* for other operations within the same transaction.*/Overridepublic void released() {super.released();if (!isOpen() this.currentConnection ! null) {if (this.connectionHandle ! null) {this.connectionHandle.releaseConnection(this.currentConnection);}this.currentConnection null;}}Overridepublic void clear() {super.clear();this.transactionActive false;this.savepointsSupported null;this.savepointCounter 0;}} 8: TransactionSynchronizationManager 这个类比较重要,也就是多线程事务的关键。 简单来说这个类里边维护了很多个线程副本(ThreadLocal)。我们都知道线程副本和当前线程绑定 通过get方法可以获取与当前线程与key对应的值。 其中包含 资源事务的同步状态名称只读标识隔离级别和真正的活跃的事务多数都是代理去除代理找到真正的非代理对象。 这里的调用的绑定资源方法key是当前数据源,value是当前连接持有者。
/*** Central delegate that manages resources and transaction synchronizations per thread.* To be used by resource management code but not by typical application code.* * pSupports one resource per key without overwriting, that is, a resource needs* to be removed before a new one can be set for the same key.* Supports a list of transaction synchronizations if synchronization is active.** pResource management code should check for thread-bound resources, e.g. JDBC* Connections or Hibernate Sessions, via {code getResource}. Such code is* normally not supposed to bind resources to threads, as this is the responsibility* of transaction managers. A further option is to lazily bind on first use if* transaction synchronization is active, for performing transactions that span* an arbitrary number of resources.** pTransaction synchronization must be activated and deactivated by a transaction* manager via {link #initSynchronization()} and {link #clearSynchronization()}.* This is automatically supported by {link AbstractPlatformTransactionManager},* and thus by all standard Spring transaction managers, such as* {link org.springframework.transaction.jta.JtaTransactionManager} and* {link org.springframework.jdbc.datasource.DataSourceTransactionManager}.** pResource management code should only register synchronizations when this* manager is active, which can be checked via {link #isSynchronizationActive};* it should perform immediate resource cleanup else. If transaction synchronization* isnt active, there is either no current transaction, or the transaction manager* doesnt support transaction synchronization.** pSynchronization is for example used to always return the same resources* within a JTA transaction, e.g. a JDBC Connection or a Hibernate Session for* any given DataSource or SessionFactory, respectively.**/// 中央对每个线程委托管理资源和事务同步。
public abstract class TransactionSynchronizationManager {private static final Log logger LogFactory.getLog(TransactionSynchronizationManager.class);private static final ThreadLocalMapObject, Object resources new NamedThreadLocal(Transactional resources);private static final ThreadLocalSetTransactionSynchronization synchronizations new NamedThreadLocal(Transaction synchronizations);private static final ThreadLocalString currentTransactionName new NamedThreadLocal(Current transaction name);private static final ThreadLocalBoolean currentTransactionReadOnly new NamedThreadLocal(Current transaction read-only status);private static final ThreadLocalInteger currentTransactionIsolationLevel new NamedThreadLocal(Current transaction isolation level);private static final ThreadLocalBoolean actualTransactionActive new NamedThreadLocal(Actual transaction active);//-------------------------------------------------------------------------// Management of transaction-associated resource handles//--------------------------------------------------------------------------/*** Return all resources that are bound to the current thread.* pMainly for debugging purposes. Resource managers should always invoke* {code hasResource} for a specific resource key that they are interested in.* return a Map with resource keys (usually the resource factory) and resource* values (usually the active resource object), or an empty Map if there are* currently no resources bound* see #hasResource*/public static MapObject, Object getResourceMap() {MapObject, Object map resources.get();return (map ! null ? Collections.unmodifiableMap(map) : Collections.emptyMap());}/*** Check if there is a resource for the given key bound to the current thread.* param key the key to check (usually the resource factory)* return if there is a value bound to the current thread* see ResourceTransactionManager#getResourceFactory()*/public static boolean hasResource(Object key) {Object actualKey TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);Object value doGetResource(actualKey);return (value ! null);}/*** Retrieve a resource for the given key that is bound to the current thread.* param key the key to check (usually the resource factory)* return a value bound to the current thread (usually the active* resource object), or {code null} if none* see ResourceTransactionManager#getResourceFactory()*/Nullablepublic 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;}/*** Actually check the value of the resource that is bound for the given key.*/Nullableprivate static Object doGetResource(Object actualKey) {MapObject, 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;}/*** Bind the given resource for the given key to the current thread.* param key the key to bind the value to (usually the resource factory)* param value the value to bind (usually the active resource object)* throws IllegalStateException if there is already a value bound to the thread* see ResourceTransactionManager#getResourceFactory()*/// 将给定的key和给定的资源和当前线程绑定。// key和value对应(通常是资源工厂)// value和通常是活跃的资源对象绑定// 如果当前线程已经有一个值绑定了,会抛出异常public static void bindResource(Object key, Object value) throws IllegalStateException {//将key去包装,直到找到非包装对象 unwrapResourceIfNecessary 中方法判断是aop还是实现InfrastructureProxy接口的,再调各种的去除包装的方法Object actualKey TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);Assert.notNull(value, Value must not be null);MapObject, Object map resources.get();// set ThreadLocal Map if none foundif (map null) {map new HashMap();resources.set(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() ]);}if (logger.isTraceEnabled()) {logger.trace(Bound value [ value ] for key [ actualKey ] to thread [ Thread.currentThread().getName() ]);}}/*** Unbind a resource for the given key from the current thread.* param key the key to unbind (usually the resource factory)* return the previously bound value (usually the active resource object)* throws IllegalStateException if there is no value bound to the thread* see ResourceTransactionManager#getResourceFactory()*/public static Object unbindResource(Object key) throws IllegalStateException {Object actualKey TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);Object value doUnbindResource(actualKey);if (value null) {throw new IllegalStateException(No value for key [ actualKey ] bound to thread [ Thread.currentThread().getName() ]);}return value;}/*** Unbind a resource for the given key from the current thread.* param key the key to unbind (usually the resource factory)* return the previously bound value, or {code null} if none bound*/Nullablepublic static Object unbindResourceIfPossible(Object key) {Object actualKey TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);return doUnbindResource(actualKey);}/*** Actually remove the value of the resource that is bound for the given key.*/Nullableprivate static Object doUnbindResource(Object actualKey) {MapObject, Object map resources.get();if (map null) {return null;}Object value map.remove(actualKey);// Remove entire ThreadLocal if empty...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() ]);}return value;}//-------------------------------------------------------------------------// Management of transaction synchronizations//-------------------------------------------------------------------------/*** Return if transaction synchronization is active for the current thread.* Can be called before register to avoid unnecessary instance creation.* see #registerSynchronization*/public static boolean isSynchronizationActive() {return (synchronizations.get() ! null);}/*** Activate transaction synchronization for the current thread.* Called by a transaction manager on transaction begin.* throws IllegalStateException if synchronization is already active*/public static void initSynchronization() throws IllegalStateException {if (isSynchronizationActive()) {throw new IllegalStateException(Cannot activate transaction synchronization - already active);}logger.trace(Initializing transaction synchronization);synchronizations.set(new LinkedHashSet());}/*** Register a new transaction synchronization for the current thread.* Typically called by resource management code.* pNote that synchronizations can implement the* {link org.springframework.core.Ordered} interface.* They will be executed in an order according to their order value (if any).* param synchronization the synchronization object to register* throws IllegalStateException if transaction synchronization is not active* see org.springframework.core.Ordered*/public static void registerSynchronization(TransactionSynchronization synchronization)throws IllegalStateException {Assert.notNull(synchronization, TransactionSynchronization must not be null);if (!isSynchronizationActive()) {throw new IllegalStateException(Transaction synchronization is not active);}synchronizations.get().add(synchronization);}/*** Return an unmodifiable snapshot list of all registered synchronizations* for the current thread.* return unmodifiable List of TransactionSynchronization instances* throws IllegalStateException if synchronization is not active* see TransactionSynchronization*/public static ListTransactionSynchronization getSynchronizations() throws IllegalStateException {SetTransactionSynchronization synchs synchronizations.get();if (synchs null) {throw new IllegalStateException(Transaction synchronization is not active);}// Return unmodifiable snapshot, to avoid ConcurrentModificationExceptions// while iterating and invoking synchronization callbacks that in turn// might register further synchronizations.if (synchs.isEmpty()) {return Collections.emptyList();}else {// Sort lazily here, not in registerSynchronization.ListTransactionSynchronization sortedSynchs new ArrayList(synchs);AnnotationAwareOrderComparator.sort(sortedSynchs);return Collections.unmodifiableList(sortedSynchs);}}/*** Deactivate transaction synchronization for the current thread.* Called by the transaction manager on transaction cleanup.* throws IllegalStateException if synchronization is not active*/public static void clearSynchronization() throws IllegalStateException {if (!isSynchronizationActive()) {throw new IllegalStateException(Cannot deactivate transaction synchronization - not active);}logger.trace(Clearing transaction synchronization);synchronizations.remove();}//-------------------------------------------------------------------------// Exposure of transaction characteristics//-------------------------------------------------------------------------/*** Expose the name of the current transaction, if any.* Called by the transaction manager on transaction begin and on cleanup.* param name the name of the transaction, or {code null} to reset it* see org.springframework.transaction.TransactionDefinition#getName()*/public static void setCurrentTransactionName(Nullable String name) {currentTransactionName.set(name);}/*** Return the name of the current transaction, or {code null} if none set.* To be called by resource management code for optimizations per use case,* for example to optimize fetch strategies for specific named transactions.* see org.springframework.transaction.TransactionDefinition#getName()*/Nullablepublic static String getCurrentTransactionName() {return currentTransactionName.get();}/*** Expose a read-only flag for the current transaction.* Called by the transaction manager on transaction begin and on cleanup.* param readOnly {code true} to mark the current transaction* as read-only; {code false} to reset such a read-only marker* see org.springframework.transaction.TransactionDefinition#isReadOnly()*/public static void setCurrentTransactionReadOnly(boolean readOnly) {currentTransactionReadOnly.set(readOnly ? Boolean.TRUE : null);}/*** Return whether the current transaction is marked as read-only.* To be called by resource management code when preparing a newly* created resource (for example, a Hibernate Session).* pNote that transaction synchronizations receive the read-only flag* as argument for the {code beforeCommit} callback, to be able* to suppress change detection on commit. The present method is meant* to be used for earlier read-only checks, for example to set the* flush mode of a Hibernate Session to FlushMode.NEVER upfront.* see org.springframework.transaction.TransactionDefinition#isReadOnly()* see TransactionSynchronization#beforeCommit(boolean)*/public static boolean isCurrentTransactionReadOnly() {return (currentTransactionReadOnly.get() ! null);}/*** Expose an isolation level for the current transaction.* Called by the transaction manager on transaction begin and on cleanup.* param isolationLevel the isolation level to expose, according to the* JDBC Connection constants (equivalent to the corresponding Spring* TransactionDefinition constants), or {code null} to reset it* see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED* see java.sql.Connection#TRANSACTION_READ_COMMITTED* see java.sql.Connection#TRANSACTION_REPEATABLE_READ* see java.sql.Connection#TRANSACTION_SERIALIZABLE* see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED* see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED* see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ* see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE* see org.springframework.transaction.TransactionDefinition#getIsolationLevel()*/public static void setCurrentTransactionIsolationLevel(Nullable Integer isolationLevel) {currentTransactionIsolationLevel.set(isolationLevel);}/*** Return the isolation level for the current transaction, if any.* To be called by resource management code when preparing a newly* created resource (for example, a JDBC Connection).* return the currently exposed isolation level, according to the* JDBC Connection constants (equivalent to the corresponding Spring* TransactionDefinition constants), or {code null} if none* see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED* see java.sql.Connection#TRANSACTION_READ_COMMITTED* see java.sql.Connection#TRANSACTION_REPEATABLE_READ* see java.sql.Connection#TRANSACTION_SERIALIZABLE* see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED* see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED* see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ* see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE* see org.springframework.transaction.TransactionDefinition#getIsolationLevel()*/Nullablepublic static Integer getCurrentTransactionIsolationLevel() {return currentTransactionIsolationLevel.get();}/*** Expose whether there currently is an actual transaction active.* Called by the transaction manager on transaction begin and on cleanup.* param active {code true} to mark the current thread as being associated* with an actual transaction; {code false} to reset that marker*/public static void setActualTransactionActive(boolean active) {actualTransactionActive.set(active ? Boolean.TRUE : null);}/*** Return whether there currently is an actual transaction active.* This indicates whether the current thread is associated with an actual* transaction rather than just with active transaction synchronization.* pTo be called by resource management code that wants to discriminate* between active transaction synchronization (with or without backing* resource transaction; also on PROPAGATION_SUPPORTS) and an actual* transaction being active (with backing resource transaction;* on PROPAGATION_REQUIRED, PROPAGATION_REQUIRES_NEW, etc).* see #isSynchronizationActive()*/public static boolean isActualTransactionActive() {return (actualTransactionActive.get() ! null);}/*** Clear the entire transaction synchronization state for the current thread:* registered synchronizations as well as the various transaction characteristics.* see #clearSynchronization()* see #setCurrentTransactionName* see #setCurrentTransactionReadOnly* see #setCurrentTransactionIsolationLevel* see #setActualTransactionActive*/public static void clear() {synchronizations.remove();currentTransactionName.remove();currentTransactionReadOnly.remove();currentTransactionIsolationLevel.remove();actualTransactionActive.remove();}
}