Spring事务03管理事务状态接口2AbstractTransactionStatus
类源码分析如下:
package org.springframework.transaction.support;import org.springframework.lang.Nullable;
import org.springframework.transaction.NestedTransactionNotSupportedException;
import org.springframework.transaction.SavepointManager;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.TransactionUsageException;
import org.springframework.transaction.support.DefaultTransactionStatus;
import org.springframework.transaction.support.SimpleTransactionStatus;
/**
* 抽象实现的{@link org.springframework.transaction.TransactionStatus}接口。
*
* 预实现本地仅回滚和已完成标志的处理,以及对底层{@link org.springframework.transaction.SavepointManager}的委托。
* 还提供在事务中保持保存点的选项。
*
* 不承担任何特定的内部事务处理,如底层事务对象,也没有事务同步机制。
*
* @author Juergen Hoeller
* @since 1.2.3
* @see #setRollbackOnly()
* @see #isRollbackOnly()
* @see #setCompleted()
* @see #isCompleted()
* @see #getSavepointManager()
* @see SimpleTransactionStatus
* @see DefaultTransactionStatus
*/
public abstract class AbstractTransactionStatus implements TransactionStatus {
private boolean rollbackOnly = false;
private boolean completed = false;
@Nullable
private Object savepoint;
//---------------------------------------------------------------------
// 处理当前事务状态
//---------------------------------------------------------------------
@Override
public void setRollbackOnly() {
this.rollbackOnly = true;
}
/**
* 通过检查
* 这个TransactionStatus的本地rollback-only标志
* 和
* 底层事务的全局rollback-only标志(如果有的话)
* 来确定rollback-only标志。
*
* @see #isLocalRollbackOnly()
* @see #isGlobalRollbackOnly()
*/
@Override
public boolean isRollbackOnly() {
return (isLocalRollbackOnly() || isGlobalRollbackOnly());
}
/**
* 通过检查这个TransactionStatus来确定只回滚标志。
* 只有当应用程序在这个TransactionStatus对象上调用{@code setRollbackOnly}时,才会返回“true”。
*/
public boolean isLocalRollbackOnly() {
return this.rollbackOnly;
}
/**
* 模板方法,用于确定基础事务的全局仅回滚标记(如果有)。
* 这个实现总是返回{@code false}。
*/
public boolean isGlobalRollbackOnly() {
return false;
}
/**
* 这个实现是空的,认为flush是无操作的。
*/
@Override
public void flush() {
}
/**
* 将此事务标记为完成,即提交或回滚。
*/
public void setCompleted() {
this.completed = true;
}
@Override
public boolean isCompleted() {
return this.completed;
}
//---------------------------------------------------------------------
// 当前保存点状态的处理
//---------------------------------------------------------------------
/**
* 为该事务设置保存点。对PROPAGATION_NESTED有用
* @see org.springframework.transaction.TransactionDefinition#PROPAGATION_NESTED
*/
protected void setSavepoint(@Nullable Object savepoint) {
this.savepoint = savepoint;
}
/**
* 获取此事务的保存点(如果有)。
*/
@Nullable
protected Object getSavepoint() {
return this.savepoint;
}
@Override
public boolean hasSavepoint() {
return (this.savepoint != null);
}
/**
* 创建一个保存点,并为事务保存它。
* 如果基础事务不支持保存点,抛出下面的异常
* @throws org.springframework.transaction.NestedTransactionNotSupportedException
*/
public void createAndHoldSavepoint() throws TransactionException {
setSavepoint(getSavepointManager().createSavepoint());
}
/**
* 回滚到为事务保留的保存点,然后立即释放保存点。
*/
public void rollbackToHeldSavepoint() throws TransactionException {
Object savepoint = getSavepoint();
if (savepoint == null) {
throw new TransactionUsageException(
"Cannot roll back to savepoint - no savepoint associated with current transaction");
}
getSavepointManager().rollbackToSavepoint(savepoint);
getSavepointManager().releaseSavepoint(savepoint);
setSavepoint(null);
}
/**
* 释放为事务保留的保存点。
*/
public void releaseHeldSavepoint() throws TransactionException {
Object savepoint = getSavepoint();
if (savepoint == null) {
throw new TransactionUsageException(
"Cannot release savepoint - no savepoint associated with current transaction");
}
getSavepointManager().releaseSavepoint(savepoint);
setSavepoint(null);
}
//---------------------------------------------------------------------
// 实现SavepointManager
//---------------------------------------------------------------------
/**
* 如果可能,此实现将底层事务委托给SavepointManager。
*
* @see #getSavepointManager()
* @see SavepointManager#createSavepoint()
*/
@Override
public Object createSavepoint() throws TransactionException {
return getSavepointManager().createSavepoint();
}
/**
* 如果可能,此实现将底层事务委托给SavepointManager。
*
* @see #getSavepointManager()
* @see SavepointManager#rollbackToSavepoint(Object)
*/
@Override
public void rollbackToSavepoint(Object savepoint) throws TransactionException {
getSavepointManager().rollbackToSavepoint(savepoint);
}
/**
* 如果可能,此实现将底层事务委托给SavepointManager。
*
* @see #getSavepointManager()
* @see SavepointManager#releaseSavepoint(Object)
*/
@Override
public void releaseSavepoint(Object savepoint) throws TransactionException {
getSavepointManager().releaseSavepoint(savepoint);
}
/**
* 如果可能,返回底层事务的SavepointManager。
*
* 默认实现总是抛出一个NestedTransactionNotSupportedException异常。
* 如果基础事务不支持保存点,抛出下面的异常
* @throws org.springframework.transaction.NestedTransactionNotSupportedException
*/
protected SavepointManager getSavepointManager() {
throw new NestedTransactionNotSupportedException("This transaction does not support savepoints");
}
}
以上是 Spring事务03管理事务状态接口2AbstractTransactionStatus 的全部内容, 来源链接: utcz.com/z/513317.html