大家好,我是烤鸭:
今天分享的是spring 多线程事务源码分析。
环境:
spring-jdbc 5.0.4.REALEASE
今天分享一下spring事务的方法,这一篇还没涉及到多线程。
强转获取当前的事务对象,看看事务对象是否有连接保持器(是否是新的事务对象)
或者连接保持器的事务同步性(默认是新的事务对象,不同步,为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 // @0
protected void doBegin(Object transaction, TransactionDefinition definition) {
//强转成数据源事务对象 @1
DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
Connection con = null;
try {
//事务对象是否有连接保持器 @2 或者
if (!txObject.hasConnectionHolder() ||
//连接保持器 是否是同步事务 @3
txObject.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 don't want to do it unnecessarily (for example if we've 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 准备开启事务,设置事务的只读标识为false
prepareTransactionalConnection(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.
*
* Note 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 doesn't 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.
*
*
The {@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规则的事务属性。基于类似EJB(Enterprise 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.
*
This 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.
*
NOTE: 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!
*
In general, use {@code PROPAGATION_SUPPORTS} with care! In particular, do
* not rely on {@code PROPAGATION_REQUIRED} or {@code PROPAGATION_REQUIRES_NEW}
* within 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.
*
Note 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.
*
NOTE: 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).
*
A {@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 需要将 TransactionManager(J2EE 的服务端指定标准) 变得可行。
// 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.
*
NOTE: 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).
*
Note that transaction synchronization is not 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 需要将 TransactionManager(J2EE 的服务端指定标准) 变得可行。
// 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.
*
Note that transaction synchronization is not 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.
*
NOTE: 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.
*
This 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.
*
This 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.
*
This 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_READ
int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ;
/**
* Indicates that dirty reads, non-repeatable reads and phantom reads
* are prevented.
*
This 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.
*
Must 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.
*
Must 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}.
*
Exclusively 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 you'd like isolation level declarations
* to get rejected when participating in an existing transaction with a different
* isolation level.
*
Note 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.
*
Must return a number of seconds, or {@link #TIMEOUT_DEFAULT}.
*
Exclusively designed for use with {@link #PROPAGATION_REQUIRED} or
* {@link #PROPAGATION_REQUIRES_NEW} since it only applies to newly started
* transactions.
*
Note 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.
*
The 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}.
*
This just serves as a hint for the actual transaction subsystem;
* it will not necessarily cause failure of write access attempts.
* A transaction manager which cannot interpret the read-only hint will
* not 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}.
*
This will be used as the transaction name to be shown in a
* transaction monitor, if applicable (for example, WebLogic's).
*
In case of Spring's 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的声明事务,暴露的名称就是(全类名+"."+方法名)
@Nullable
String 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}.
*
* Allows for programmatic management of JDBC {@link java.sql.Savepoint Savepoints}.
* Spring's {@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.
*
* Note 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.
*
*
This interface is inspired by JDBC 3.0's 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 don't need anymore via {@code releaseSavepoint}.
*
Note 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.
*
The savepoint will not 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.
*
Note that most transaction managers will automatically release
* savepoints on transaction completion.
*
Implementations 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.
*
* Autodetected 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会话。(清理缓存区的资源)
@Override
void flush();
}
@3: isSynchronizedWithTransaction() 是ResourceHolderSupport 类中的方法
/**
* Convenient base class for resource holders.
*
* Features 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 Spring's 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}.
*
* Used internally by Spring's {@link org.springframework.jdbc.core.JdbcTemplate},
* Spring's JDBC operation objects and the JDBC {@link DataSourceTransactionManager}.
* Can also be used directly in application code.
*/
//工具类提供静态方法,从数据源中获取jdbc连接,包括对spring管理的事务拦截的特别支撑,
//在被spring的JdbcTemplate,spring的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.
*
Is 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.
*
Is 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).
*
Directly 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 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
//只读不支持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 Spring's 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.
*
Directly 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)) {
// It's the transactional Connection: Don't close it.
conHolder.released();
return;
}
}
logger.debug("Returning JDBC Connection to DataSource");
doCloseConnection(con, dataSource);
}
/**
* Close the Connection, unless a {@link SmartDataSource} doesn't 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);
}
@Override
public int getOrder() {
return this.order;
}
@Override
public void suspend() {
if (this.holderActive) {
TransactionSynchronizationManager.unbindResource(this.dataSource);
if (this.connectionHolder.hasConnection() && !this.connectionHolder.isOpen()) {
// Release Connection on suspend if the application doesn't 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);
}
}
}
@Override
public void resume() {
if (this.holderActive) {
TransactionSynchronizationManager.bindResource(this.dataSource, this.connectionHolder);
}
}
@Override
public 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);
}
}
}
@Override
public void afterCompletion(int status) {
// If we haven't 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.
* The 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.
*
The "SET TRANSACTION READ ONLY" is understood by Oracle, MySQL and Postgres
* and may work with other databases as well. If you'd 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.
*
* Inherits rollback-only support for nested JDBC transactions
* and reference count functionality from the base class.
*
*
Note: 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_";
@Nullable
private ConnectionHandle connectionHandle;
@Nullable
private Connection currentConnection;
private boolean transactionActive = false;
@Nullable
private 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.
*/
@Nullable
public 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}.
*
Used 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.
*
This 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.
*
This 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.
*/
@Override
public void released() {
super.released();
if (!isOpen() && this.currentConnection != null) {
if (this.connectionHandle != null) {
this.connectionHandle.releaseConnection(this.currentConnection);
}
this.currentConnection = null;
}
}
@Override
public 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.
*
* Supports 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.
*
*
Resource 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.
*
*
Transaction 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}.
*
*
Resource 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
* isn't active, there is either no current transaction, or the transaction manager
* doesn't support transaction synchronization.
*
*
Synchronization 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 ThreadLocal