spring事务三大接口:
1.TransactionStatus
spring对事务封装的接口
public interface TransactionStatus extends SavepointManager, Flushable {
boolean isNewTransaction();
boolean hasSavepoint();
void setRollbackOnly();
boolean isRollbackOnly();
@Override
void flush();
boolean isCompleted();
}
public abstract class AbstractTransactionStatus implements TransactionStatus {
//当在同一个事务中,内侧事务回滚,会把外侧rollbackOnly设置为true
private boolean rollbackOnly = false;
//当前事务是否完成
private boolean completed = false;
@Nullable
private Object savepoint;
@Override
public void setRollbackOnly() {
this.rollbackOnly = true;
}
@Override
public boolean isRollbackOnly() {
return (isLocalRollbackOnly() || isGlobalRollbackOnly());
}
public boolean isLocalRollbackOnly() {
return this.rollbackOnly;
}
public boolean isGlobalRollbackOnly() {
return false;
}
@Override
public void flush() {
}
public void setCompleted() {
this.completed = true;
}
@Override
public boolean isCompleted() {
return this.completed;
}
protected void setSavepoint(@Nullable Object savepoint) {
this.savepoint = savepoint;
}
@Nullable
protected Object getSavepoint() {
return this.savepoint;
}
@Override
public boolean hasSavepoint() {
return (this.savepoint != null);
}
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);
}
@Override
public Object createSavepoint() throws TransactionException {
return getSavepointManager().createSavepoint();
}
@Override
public void rollbackToSavepoint(Object savepoint) throws TransactionException {
getSavepointManager().rollbackToSavepoint(savepoint);
}
@Override
public void releaseSavepoint(Object savepoint) throws TransactionException {
getSavepointManager().releaseSavepoint(savepoint);
}
protected SavepointManager getSavepointManager() {
throw new NestedTransactionNotSupportedException("This transaction does not support savepoints");
}
}
public class DefaultTransactionStatus extends AbstractTransactionStatus {
/**
* 代表一个事务对象
* DataSourceTransactionManager事务管理器使用的是其内部类DataSourceTransactionObject
*/
@Nullable
private final Object transaction;
/**
* 代表是不是一个新事务(空事务或真实存在的事务)
* 注: 值为true时不代表开启一个真实存在的事务
* 当指定事务传播行为以非事务方式运行的时候,Spring也会将这个值置为true,但是transaction为null
* 以下几种情况newTransaction = true
* 1. 开启一个真实存在的事务,必须是新的,参与外部事务的不算。 (transaction != null)
* 2. 开启一个空事务,必须是新开的。 (transaction = null)
* 2.1 不存在外部真实事务
* 传播行为可能的值: PROPAGATION_NOT_SUPPORTED,PROPAGATION_SUPPORTS,PROPAGATION_NEVER
* 2.2 存在外部真实事务
* 传播行为的值: PROPAGATION_NOT_SUPPORTED(因为会将外部事务挂起,以非事务方式运行)
* ==========================================================================
* 判断是否存在一个新的真实事务: newTransaction == true && transaction != null
* 判断存在一个空事务(非事务执行方式): newTransaction == true && transaction != null
* 判断此事务是否参与到外部事务: newTransaction == false && transaction != null
* 判断存在一个事务(新的真实事务或者参与外部事务):transaction != null
*/
private final boolean newTransaction;
/**
* 值为true,事务管理器需要为当前线程绑定当前事务的属性以及TransactionSynchronization回调接口
* 这个值会受到AbstractPlatformTransactionManager类中定义的transactionSynchronization字段 * 影响。
* transactionSynchronization可能的值为AbstractPlatformTransactionManager类定义的
* 三个常量,默认为SYNCHRONIZATION_ALWAYS
* 1. SYNCHRONIZATION_ALWAYS:总是激活事务同步(无论是空事务还是真实事务)
* 2. SYNCHRONIZATION_ON_ACTUAL_TRANSACTION: 只有真实事务才激活事务同步
* 3. SYNCHRONIZATION_NEVER:不激活事务同步
* 这个字段的作用:
* 如果为true,那么会将当前的事务属性(传播行为、隔离级别、事务名字、事务只读性、事务是否存活-用以区分 * 是空事务还是真实的事务)以及TransactionSynchronization回调接口
* ==========================================================================
* 在DataSourceTransactionManager事务管理器中并且transactionSynchronization为默认值时:
* newSynchronization=true的情况
* 1. 外部没有真实事务时
* 1.1 调用事务接口的getTransaction方法开启一个事务(无论是空事务还是真实事务)
* 2. 外部存在真实事务
* 2.1 事务传播行为是PROPAGATION_NOT_SUPPORTED、PROPAGATION_REQUIRES_NEW时。
* 因为这两个传播行为会挂起外部事务,而开一个新的事务(对应空事务,真实事务)
*/
private final boolean newSynchronization;
/**
* 对于只读查询,可以指定事务类型为readonly,即只读事务。
* 由于只读事务不存在数据的修改,因此数据库将会为只读事务提供一些优化手段,例如Oracle对于只读事务,不启动回滚段,不记录回滚log。
*
* (1)在JDBC中,指定只读事务的办法为: connection.setReadOnly(true);
*
* (2)在Hibernate中,指定只读事务的办法为: session.setFlushMode(FlushMode.NEVER);
* 此时,Hibernate也会为只读事务提供Session方面的一些优化手段
*
* (3)在Spring的Hibernate封装中,指定只读事务的办法为: bean配置文件中,prop属性增加“readOnly”
* 或者用注解方式@Transactional(readOnly=true)
*/
private final boolean readOnly;
private final boolean debug;
//前面事务挂起的信息
@Nullable
private final Object 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;
}
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;
}
public boolean isDebug() {
return this.debug;
}
@Nullable
public Object getSuspendedResources() {
return this.suspendedResources;
}
@Override
public boolean isGlobalRollbackOnly() {
return ((this.transaction instanceof SmartTransactionObject) &&
((SmartTransactionObject) this.transaction).isRollbackOnly());
}
@Override
public void flush() {
if (this.transaction instanceof SmartTransactionObject) {
((SmartTransactionObject) this.transaction).flush();
}
}
/**
* This implementation exposes the {@link SavepointManager} interface
* of the underlying transaction object, if any.
* @throws NestedTransactionNotSupportedException if savepoints are not supported
* @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;
}
/**
* Return whether the underlying transaction implements the {@link SavepointManager}
* interface and therefore supports savepoints.
* @see #getTransaction()
* @see #getSavepointManager()
*/
public boolean isTransactionSavepointManager() {
return (this.transaction instanceof SavepointManager);
}
2.TransactionDefinition
public interface TransactionDefinition {
int PROPAGATION_REQUIRED = 0;
int PROPAGATION_SUPPORTS = 1;
int PROPAGATION_REQUIRES_NEW = 3;
int PROPAGATION_NOT_SUPPORTED = 4;
int PROPAGATION_NEVER = 5;
int PROPAGATION_NESTED = 6;
int ISOLATION_DEFAULT = -1;
int ISOLATION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED;
int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED;
int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ;
int ISOLATION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE;
int TIMEOUT_DEFAULT = -1;
int getPropagationBehavior();
int getIsolationLevel();
int getTimeout();
boolean isReadOnly();
@Nullable
String getName();
事务的隔离级别:
读未提交(Read Uncommitted)读提交(Read Committed)可重复读(Repeated Read)串行化(Serializable)
事务的传播行为
事务传播行为类型 | 说明 |
PROPAGATION_REQUIRED | 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。 |
PROPAGATION_SUPPORTS | 支持当前事务,如果当前没有事务,就以非事务方式执行。 |
PROPAGATION_MANDATORY | 使用当前的事务,如果当前没有事务,就抛出异常。 |
PROPAGATION_REQUIRES_NEW | 新建事务,如果当前存在事务,把当前事务挂起。 |
PROPAGATION_NOT_SUPPORTED | 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 |
PROPAGATION_NEVER | 以非事务方式执行,如果当前存在事务,则抛出异常。 |
PROPAGATION_NESTED | 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。 |
3.PlatformTransactionManager
平台事务管理器:
public interface PlatformTransactionManager {
TransactionStatus getTransaction(@Nullable TransactionDefinition definition)
throws TransactionException;
void commit(TransactionStatus status) throws TransactionException;
void rollback(TransactionStatus status) throws TransactionException;
}