spring tx——TransactionManger
2021-05-01 04:30
标签:set 开启 value bug abstract 存在 标记 poi tee 定义事务属性,包括传播级别、隔离级别、名称、超时、只读等 事务状态,包含事务对象(jdbc为DataSourceTransactionObject),是否新事务、是否新同步对象、是否只读、是否回滚、是否完成、事务中断对象(SuspendedResourcesHolder实现事务中断,保存中断事务的信息)、保存点对象(jdbc为Savepoint,实现嵌套事务) jdbc中事务对象。 持有一个ConnectionHolder对象,而ConnectionHolder持有Connection对象。 用于实现事务挂起,持有一个中断事务suspendedResources(jdbc为ConnectionHolder),中断同步对象集合suspendedSynchronizations。 保存点用于实现嵌套事务,只有jdbc才有 AbstractPlatformTransactionManager:实现事务的获取、提交、回滚等主要逻辑 传播级别——Propagation processCommit方法: processRollback方法: spring tx——TransactionManger 标签:set 开启 value bug abstract 存在 标记 poi tee 原文地址:https://www.cnblogs.com/wuweishuo/p/13221009.htmlTransactionDefinition——事务定义
TransactionStatus——事务状态
DataSourceTransactionObject——事务对象
SuspendedResourcesHolder——中断事务对象
SavePoint——保存点
TransactionManger——事务管理器
getTransaction——获取事务
public final TransactionStatus getTransaction(@Nullable TransactionDefinition definition)
throws TransactionException {
TransactionDefinition def = (definition != null ? definition : TransactionDefinition.withDefaults());
//1.封装transaction对象并获取当前事务的ConnectionHolder
Object transaction = doGetTransaction();
//2.当前存在事务,根据事务传播级别处理
if (isExistingTransaction(transaction)) {
return handleExistingTransaction(def, transaction, debugEnabled);
}
if (def.getTimeout()
commit——提交事务
public final void commit(TransactionStatus status) throws TransactionException {
//1.TransactionStatus设置了回滚,执行回滚操作
DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status;
if (defStatus.isLocalRollbackOnly()) {
if (defStatus.isDebug()) {
logger.debug("Transactional code has requested rollback");
}
processRollback(defStatus, false);
return;
}
//2.TransactionStatus的ConnectionHolder设置了回滚,执行回滚操作
if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {
if (defStatus.isDebug()) {
logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");
}
processRollback(defStatus, true);
return;
}
//3.执行提交操作
processCommit(defStatus);
}
private void processCommit(DefaultTransactionStatus status) throws TransactionException {
try {
boolean beforeCompletionInvoked = false;
try {
boolean unexpectedRollback = false;
prepareForCommit(status);
//触发TransactionSynchronization的beforeCommit和beforeCompletion
triggerBeforeCommit(status);
triggerBeforeCompletion(status);
beforeCompletionInvoked = true;
//有保存点,即嵌套事务,释放保存点
if (status.hasSavepoint()) {
if (status.isDebug()) {
logger.debug("Releasing transaction savepoint");
}
unexpectedRollback = status.isGlobalRollbackOnly();
status.releaseHeldSavepoint();
}
//如果是事务最外层,提交事务
else if (status.isNewTransaction()) {
if (status.isDebug()) {
logger.debug("Initiating transaction commit");
}
unexpectedRollback = status.isGlobalRollbackOnly();
doCommit(status);
}
//默认为false
else if (isFailEarlyOnGlobalRollbackOnly()) {
unexpectedRollback = status.isGlobalRollbackOnly();
}
if (unexpectedRollback) {
throw new UnexpectedRollbackException(
"Transaction silently rolled back because it has been marked as rollback-only");
}
}
catch (UnexpectedRollbackException ex) {
// can only be caused by doCommit
//触发TransactionSynchronization的afterCompletion
triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);
throw ex;
}
catch (TransactionException ex) {
// can only be caused by doCommit
//默认为false
if (isRollbackOnCommitFailure()) {
doRollbackOnCommitException(status, ex);
}
else {
//触发TransactionSynchronization的afterCompletion
triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
}
throw ex;
}
catch (RuntimeException | Error ex) {
//是否触发过TransactionSynchronization的beforeCompletion
if (!beforeCompletionInvoked) {
triggerBeforeCompletion(status);
}
//回滚事务
doRollbackOnCommitException(status, ex);
throw ex;
}
//触发TransactionSynchronization的afterCommit和afterCompletion
try {
triggerAfterCommit(status);
}
finally {
triggerAfterCompletion(status, TransactionSynchronization.STATUS_COMMITTED);
}
}
finally {
//有中断事务恢复中断事务,没有重置TransactionSynchronizationManager
cleanupAfterCompletion(status);
}
}
rollback——回滚事务
public final void rollback(TransactionStatus status) throws TransactionException {
DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status;
processRollback(defStatus, false);
}
private void processRollback(DefaultTransactionStatus status, boolean unexpected) {
try {
boolean unexpectedRollback = unexpected;
try {
//触发TransactionSynchronization的beforeCompletion
triggerBeforeCompletion(status);
//有保存点,即嵌套事务,回滚到保存点
if (status.hasSavepoint()) {
if (status.isDebug()) {
logger.debug("Rolling back transaction to savepoint");
}
status.rollbackToHeldSavepoint();
}
//如果是事务最外层,回滚事务
else if (status.isNewTransaction()) {
if (status.isDebug()) {
logger.debug("Initiating transaction rollback");
}
doRollback(status);
}
else {
// 如果事务标记为回滚,将ConnectionHolder标记为回滚
if (status.hasTransaction()) {
if (status.isLocalRollbackOnly() || isGlobalRollbackOnParticipationFailure()) {
doSetRollbackOnly(status);
}
}
// Unexpected rollback only matters here if we‘re asked to fail early
if (!isFailEarlyOnGlobalRollbackOnly()) {
unexpectedRollback = false;
}
}
}
catch (RuntimeException | Error ex) {
//触发TransactionSynchronization的afterCompletion
triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
throw ex;
}
//触发TransactionSynchronization的afterCompletion
triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);
// Raise UnexpectedRollbackException if we had a global rollback-only marker
if (unexpectedRollback) {
throw new UnexpectedRollbackException(
"Transaction rolled back because it has been marked as rollback-only");
}
}
finally {
//有中断事务恢复中断事务,没有重置TransactionSynchronizationManager
cleanupAfterCompletion(status);
}
}
上一篇:java 线程池 实现
文章标题:spring tx——TransactionManger
文章链接:http://soscw.com/index.php/essay/80713.html