Spring事务03AbstractPlatformTransactionManager

编程

挑几个常见的实现类,Spring 事务管理重要的是 DataSourceTransactionManager

 

/**

* Abstract base class that implements Spring"s standard transaction workflow,

* serving as basis for concrete platform transaction managers like

* {@link org.springframework.transaction.jta.JtaTransactionManager}.

*

* <p>This base class provides the following workflow handling:

* <ul>

* <li>determines if there is an existing transaction;

* <li>applies the appropriate propagation behavior;

* <li>suspends and resumes transactions if necessary;

* <li>checks the rollback-only flag on commit;

* <li>applies the appropriate modification on rollback

* (actual rollback or setting rollback-only);

* <li>triggers registered synchronization callbacks

* (if transaction synchronization is active).

* </ul>

*

* <p>Subclasses have to implement specific template methods for specific

* states of a transaction, e.g.: begin, suspend, resume, commit, rollback.

* The most important of them are abstract and must be provided by a concrete

* implementation; for the rest, defaults are provided, so overriding is optional.

*

* <p>Transaction synchronization is a generic mechanism for registering callbacks

* that get invoked at transaction completion time. This is mainly used internally

* by the data access support classes for JDBC, Hibernate, JPA, etc when running

* within a JTA transaction: They register resources that are opened within the

* transaction for closing at transaction completion time, allowing e.g. for reuse

* of the same Hibernate Session within the transaction. The same mechanism can

* also be leveraged for custom synchronization needs in an application.

*

* <p>The state of this class is serializable, to allow for serializing the

* transaction strategy along with proxies that carry a transaction interceptor.

* It is up to subclasses if they wish to make their state to be serializable too.

* They should implement the {@code java.io.Serializable} marker interface in

* that case, and potentially a private {@code readObject()} method (according

* to Java serialization rules) if they need to restore any transient state.

*

* @author Juergen Hoeller

* @since 28.03.2003

* @see #setTransactionSynchronization

* @see TransactionSynchronizationManager

* @see org.springframework.transaction.jta.JtaTransactionManager

*/

@SuppressWarnings("serial")

public abstract class AbstractPlatformTransactionManager implements PlatformTransactionManager, Serializable {

/**

* Return a transaction object for the current transaction state.

* 返回当前事务状态的事务对象

*/

protected abstract Object doGetTransaction() throws TransactionException;

/**

* Begin a new transaction with semantics according to the given transaction definition.

* 根据给定的事务定义使用语义开始一个新的事务

*/

protected abstract void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException;

/**

* Perform an actual commit of the given transaction.

* 执行给定事务的实际提交

*/

protected abstract void doCommit(DefaultTransactionStatus status) throws TransactionException;

/**

* Perform an actual rollback of the given transaction.

* 执行给定事务的实际回滚

*/

protected abstract void doRollback(DefaultTransactionStatus status) throws TransactionException;

}

翻译如下:

实现了Spring标准事务工作流的抽象基类,作为具体平台事务管理器的基础,

如{@link org.springframework.transaction.jta.JtaTransactionManager}。

这个基类提供了以下的工作流程处理:

1 确定是否存在现有事务;

2 应用合适的传播行为;

3 在必要时暂停并恢复事务;

4 在提交时检查仅回滚标志;

5 在回滚时应用了适当的修改(实际回滚或只设置回滚);

6 触发注册的同步回调(如果事务同步是活动的)。

子类必须实现特定的模板方法事务的状态,例如:begin(开始)、suspend(暂停)、resume(恢复)、commit(提交)、rollback(回滚)。

其中最重要的是抽象的,必须由具体的子类来提供实现;对于其余部分,则提供默认值,因此覆盖是可选的。

事务同步是注册回调的通用机制

在事务完成时调用。这主要用于内部

由数据访问支持类JDBC、Hibernate、JPA等运行时

在JTA事务中:它们注册在

在事务完成时关闭的事务,例如允许重用

事务中的相同Hibernate会话。同样的机制可以

也可用于应用程序中的自定义同步需求。

这个类的状态是可序列化的,允许序列化

事务策略以及携带事务拦截器的代理。

如果子类希望它们的状态也是可序列化的,则由子类决定。

它们应该实现{@code java.io。序列化}标记接口

在这种情况下,可能会有一个私有的{@code readObject()}方法(根据到Java序列化规则),如果他们需要恢复任何瞬态状态。

以上是 Spring事务03AbstractPlatformTransactionManager 的全部内容, 来源链接: utcz.com/z/513282.html

回到顶部