共计 3170 个字符,预计需要花费 8 分钟才能阅读完成。
接上节事务执行流程,这是第 3 点的解析。
创立事务次要两局部:
- 获取事务状态
- 构建事务信息
获取事务状态
代码如下:
@Override
public final TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
//1. 获取事务
Object transaction = doGetTransaction();
// Cache debug flag to avoid repeated checks.
boolean debugEnabled = logger.isDebugEnabled();
if (definition == null) {
// Use defaults if no transaction definition given.
definition = new DefaultTransactionDefinition();}
// 判断以后线程是否存在事务,判断根据为以后线程记录连贯不为空且连贯中的 (connectionHolder) 中的 transactionActive 属性不为空
if (isExistingTransaction(transaction)) {
// Existing transaction found -> check propagation behavior to find out how to behave.
return handleExistingTransaction(definition, transaction, debugEnabled);
}
// Check definition settings for new transaction.
// 事务超时设置验证
if (definition.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {throw new InvalidTimeoutException("Invalid transaction timeout", definition.getTimeout());
}
// No existing transaction found -> check propagation behavior to find out how to proceed.
// 如果以后线程不存在事务,然而 @Transactional 却申明事务为 PROPAGATION_MANDATORY 抛出异样
if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY) {
throw new IllegalTransactionStateException("No existing transaction found for transaction marked with propagation'mandatory'");
}
// 如果以后线程不存在事务,PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW、PROPAGATION_NESTED 都得创立事务
else if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED ||
definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW ||
definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) {
// 空挂起
SuspendedResourcesHolder suspendedResources = suspend(null);
if (debugEnabled) {logger.debug("Creating new transaction with name [" + definition.getName() + "]:" + definition);
}
try {
// 默认返回 true
boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
// 构建事务状态
DefaultTransactionStatus status = newTransactionStatus(definition, transaction, true, newSynchronization, debugEnabled, suspendedResources);
// 结构 transaction、包含设置 connectionHolder、隔离级别、timeout
// 如果是新事务,绑定到以后线程
doBegin(transaction, definition);
// 新事务同步设置,针对以后线程
prepareSynchronization(status, definition);
return status;
}
catch (RuntimeException ex) {resume(null, suspendedResources);
throw ex;
}
catch (Error err) {resume(null, suspendedResources);
throw err;
}
}
else {
// Create "empty" transaction: no actual transaction, but potentially synchronization.
if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT && logger.isWarnEnabled()) {
logger.warn("Custom isolation level specified but no actual transaction initiated;" +
"isolation level will effectively be ignored:" + definition);
}
// 申明事务是 PROPAGATION_SUPPORTS
boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
return prepareTransactionStatus(definition, null, true, newSynchronization, debugEnabled, null);
}
}
构建事务信息
- 获取事务,创立对应的事务实例,这里应用的是 DataSourceTransactionManager 中的 doGetTransaction 办法,创立基于 JDBC 的事务实例,如果以后线程中存在对于 dataSoruce 的连贯,那么间接应用。这里有一个对保留点的设置,是否开启容许保留点取决于是否设置了容许嵌入式事务。DataSourceTransactionManager 默认是开启的。
- 如果当先线程存在事务,则转向嵌套的事务处理。是否存在事务在 DataSourceTransactionManager 的 isExistingTransaction 办法中
- 事务超时设置验证
- 事务 PropagationBehavior 属性的设置验证
- 构建 DefaultTransactionStatus。
- 欠缺 transaction,包含设置 connectionHolder、隔离级别、timeout,如果是新事务,绑定到以后线程
- 将事务信息记录在以后线程中
正文完