Spring事务框架之TransactionStatus源码解析
作者:福
Spring事务框架
本文来分析TransactionStatus。
用来记录事务执行过程中的状态的,最终决定该事务能否提交、是否需要回滚等。
先来看一下TransactionStatus的类结构:
TransactionStatus
public interface TransactionStatus extends TransactionExecution, SavepointManager, Flushable{ /** * Return whether this transaction internally carries a savepoint, * that is, has been created as nested transaction based on a savepoint. * <p>This method is mainly here for diagnostic purposes, alongside * {@link #isNewTransaction()}. For programmatic handling of custom * savepoints, use the operations provided by {@link SavepointManager}. * @see #isNewTransaction() * @see #createSavepoint() * @see #rollbackToSavepoint(Object) * @see #releaseSavepoint(Object) */ boolean hasSavepoint(); Flush the underlying session to the datastore, if applicable: for example, all affected Hibernate/JPA sessions. This is effectively just a hint and may be a no-op if the underlying transaction manager does not have a flush concept. A flush signal may get applied to the primary resource or to transaction synchronizations, depending on the underlying resource. @Override void flush(); }
他只定义了两个方法:
- hasSavepoint:返回这个事务是否包含了savepoint,也就是说,是否基于嵌套事务创建了一个savepoint。savepoint的概念前面的文章已经分析过。
- flush:这个应该是和Hibernate或JPA相关的,具体作用暂时不管了,不研究Hibernate相关的东西。
没了。
但是,这个接口继承了3个接口:TransactionExecution, SavepointManager, Flushable,我们简单看一眼:
TransactionExecution
这个接口很简单,是事务状态的一个通用接口,定义了当前事务是否是一个新事务的获取方法、设置当前事务为回滚状态、获取事务是否已经完成的方法等。
/** * Common representation of the current state of a transaction. * Serves as base interface for {@link TransactionStatus} as well as * {@link ReactiveTransaction}. * * @author Juergen Hoeller * @since 5.2 */ public interface TransactionExecution { /** * Return whether the present transaction is new; otherwise participating * in an existing transaction, or potentially not running in an actual * transaction in the first place. */ boolean isNewTransaction(); /** * Set the transaction rollback-only. This instructs the transaction manager * that the only possible outcome of the transaction may be a rollback, as * alternative to throwing an exception which would in turn trigger a rollback. */ void setRollbackOnly(); /** * Return whether the transaction has been marked as rollback-only * (either by the application or by the transaction infrastructure). */ boolean isRollbackOnly(); /** * Return whether this transaction is completed, that is, * whether it has already been committed or rolled back. */ boolean isCompleted(); }
SavepointManager
提供3个方法:创建保存点、回滚到保存点、释放保存点。
Object createSavepoint() throws TransactionException; void rollbackToSavepoint(Object savepoint) throws TransactionException; void releaseSavepoint(Object savepoint) throws TransactionException;
Flushable
不说了,就是上面的那个flush方法。
AbstactTransactionStatus & DefaultTransactionStatus
AbstactTransactionStatus持有事务的几个重要状态,业务执行后,Spring事务管理器需要通过状态判断事务是提交或者是回滚。
private boolean rollbackOnly = false; private boolean completed = false; @Nullable private Object savepoint;
Spring事务管理机制中TransactionStatus的最终落地实现是DefaultTransactionStatus,代码就不贴出了,比较简单。
其实我们通过对TransactioStatus的分析能够得出一个结论,那就是有savepoint的事务的回滚是通过TransactionStatus实现的。
TransactionStatus持有事务对象transaction,事务保存点savepoint是保存在transaction中,最终通过调用transaction的rollbackToSavepoint回滚事务到存储点。
好了,TransactionStatus就分析到这儿。
以上就是Spring事务框架之TransactionStatus的详细内容,更多关于Spring事务TransactionStatus的资料请关注脚本之家其它相关文章!