Spring事务03管理事务状态接口3DefaultTransactionStatus
2 类分析
/* * Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.test.transaction.test;
package org.springframework.transaction.support;
import org.springframework.lang.Nullable;
import org.springframework.transaction.NestedTransactionNotSupportedException;
import org.springframework.transaction.SavepointManager;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.transaction.support.AbstractTransactionStatus;
import org.springframework.transaction.support.SimpleTransactionStatus;
import org.springframework.transaction.support.SmartTransactionObject;
import org.springframework.util.Assert;
/**
* 管理事务状态的实现。
*
* 该类是{@link org.springframework.transaction.TransactionStatus}接口的默认实现,
* 由{@link AbstractPlatformTransactionManager}使用。
* 基于底层“事务对象”的概念。
*
* 包含{@link AbstractPlatformTransactionManager}内部需要的所有状态信息,
* 包括由具体事务管理器实现确定的通用事务对象。
*
* 支持将与保存点相关的方法委托给实现{@link SavepointManager}接口的事务对象。
*
* 注意:这并不打算与其他的PlatformTransactionManager实现一起使用,
* 特别是对于测试环境中的模拟事务管理器。
* 使用另一个{@link SimpleTransactionStatus}类或普通{@link org.springframework.transaction.TransactionStatus}接口。
*
* @see AbstractPlatformTransactionManager
* @see SavepointManager
* @see #getTransaction
* @see #createSavepoint
* @see #rollbackToSavepoint
* @see #releaseSavepoint
* @see SimpleTransactionStatus
*/
public class DefaultTransactionStatus extends AbstractTransactionStatus {
@Nullable
private final Object transaction;
private final boolean newTransaction;
private final boolean newSynchronization;
private final boolean readOnly;
private final boolean debug;
@Nullable
private final Object suspendedResources;
/**
* 创建一个新的{@code DefaultTransactionStatus}实例。
*
* @param transaction 可以保存内部事务实现状态的基础事务对象
* @param newTransaction 如果事务是新的,则参与现有事务
* @param newSynchronization 如果为给定的事务打开了新的事务同步
* @param readOnly 事务是否标记为只读
* @param debug 应该启用调试日志来处理这个事务吗?
* 在这里缓存它可以防止重复调用日志记录系统,询问是否应该启用调试日志记录。
* @param suspendedResources 资源的持有者,该资源已被暂停,如果有的话
*/
public DefaultTransactionStatus(
@Nullable Object transaction, boolean newTransaction, boolean newSynchronization,
boolean readOnly, boolean debug, @Nullable Object suspendedResources) {
this.transaction = transaction;
this.newTransaction = newTransaction;
this.newSynchronization = newSynchronization;
this.readOnly = readOnly;
this.debug = debug;
this.suspendedResources = suspendedResources;
}
/**
* 返回底层事务对象。
* @throws IllegalStateException 如果没有活动的事务
*/
public Object getTransaction() {
Assert.state(this.transaction != null, "No transaction active");
return this.transaction;
}
/**
* 返回是否有实际活动的事务。
*/
public boolean hasTransaction() {
return (this.transaction != null);
}
@Override
public boolean isNewTransaction() {
return (hasTransaction() && this.newTransaction);
}
/**
* 返回为该事务是否打开了新的事务同步。
*/
public boolean isNewSynchronization() {
return this.newSynchronization;
}
/**
* 返回此事务是否定义为只读事务
*/
public boolean isReadOnly() {
return this.readOnly;
}
/**
* 返回该事务的进程是否被调试。
* 这被{@link AbstractPlatformTransactionManager}用作优化,
* 为了防止重复调用{@code logger.isDebugEnabled()}。
* 不适合客户端代码。
*/
public boolean isDebug() {
return this.debug;
}
/**
* 返回此事务中已挂起的资源的持有者(如果有)。
*/
@Nullable
public Object getSuspendedResources() {
return this.suspendedResources;
}
//---------------------------------------------------------------------
// 通过底层事务对象启用功能
//---------------------------------------------------------------------
/**
* 如果事务对象实现{@link SmartTransactionObject}接口,则通过检查事务对象来确定仅回滚标志。
*
* 如果全局事务本身仅由事务协调器标记为rollback,则返回{@code true},例如在超时的情况下。
*
* @see SmartTransactionObject#isRollbackOnly()
*/
@Override
public boolean isGlobalRollbackOnly() {
return ((this.transaction instanceof SmartTransactionObject) &&
((SmartTransactionObject) this.transaction).isRollbackOnly());
}
/**
* 将刷新委托给事务对象,前提是后者实现{@link SmartTransactionObject}接口。
*
* @see SmartTransactionObject#flush()
*/
@Override
public void flush() {
if (this.transaction instanceof SmartTransactionObject) {
((SmartTransactionObject) this.transaction).flush();
}
}
/**
* 这个实现公开了底层事务对象的{@link SavepointManager}接口(如果有的话)。
* @throws NestedTransactionNotSupportedException 如果不支持保存点
* @see #isTransactionSavepointManager()
*/
@Override
protected SavepointManager getSavepointManager() {
Object transaction = this.transaction;
if (!(transaction instanceof SavepointManager)) {
throw new NestedTransactionNotSupportedException(
"Transaction object [" + this.transaction + "] does not support savepoints");
}
return (SavepointManager) transaction;
}
/**
* 返回基础事务是否实现{@link SavepointManager}接口,因此是否支持保存点。
* @see #getTransaction()
* @see #getSavepointManager()
*/
public boolean isTransactionSavepointManager() {
return (this.transaction instanceof SavepointManager);
}
}
以上是 Spring事务03管理事务状态接口3DefaultTransactionStatus 的全部内容, 来源链接: utcz.com/z/513316.html