接口 TransactionOperations
- 所有已知实现类:
TransactionTemplate
public interface TransactionOperations
Interface specifying basic transaction execution operations. Implemented byTransactionTemplate
. Not often used directly, but a useful option to enhance testability, as it can easily be mocked or stubbed.- 从以下版本开始:
- 2.0.4
- 作者:
- Juergen Hoeller
方法概要
所有方法 静态方法 实例方法 抽象方法 默认方法 修饰符和类型 方法 说明 <T> T
execute(TransactionCallback<T> action)
Execute the action specified by the given callback object within a transaction.default void
executeWithoutResult(Consumer<TransactionStatus> action)
Execute the action specified by the givenRunnable
within a transaction.static TransactionOperations
withoutTransaction()
Return an implementation of theTransactionOperations
interface which executes a givenTransactionCallback
without an actual transaction.
方法详细资料
execute
@Nullable <T> T execute(TransactionCallback<T> action) throws TransactionException
Execute the action specified by the given callback object within a transaction.Allows for returning a result object created within the transaction, that is, a domain object or a collection of domain objects. A RuntimeException thrown by the callback is treated as a fatal exception that enforces a rollback. Such an exception gets propagated to the caller of the template.
- 参数:
action
- the callback object that specifies the transactional action- 返回:
- a result object returned by the callback, or
null
if none - 抛出:
TransactionException
- in case of initialization, rollback, or system errorsRuntimeException
- if thrown by the TransactionCallback- 另请参阅:
executeWithoutResult(Consumer)
executeWithoutResult
default void executeWithoutResult(Consumer<TransactionStatus> action) throws TransactionException
Execute the action specified by the givenRunnable
within a transaction.If you need to return an object from the callback or access the
TransactionStatus
from within the callback, useexecute(TransactionCallback)
instead.This variant is analogous to using a
TransactionCallbackWithoutResult
but with a simplified signature for common cases - and conveniently usable with Java 8 lambda expressions.- 参数:
action
- the Runnable that specifies the transactional action- 抛出:
TransactionException
- in case of initialization, rollback, or system errorsRuntimeException
- if thrown by the Runnable- 从以下版本开始:
- 5.2
- 另请参阅:
execute(TransactionCallback)
,TransactionCallbackWithoutResult
withoutTransaction
static TransactionOperations withoutTransaction()
Return an implementation of theTransactionOperations
interface which executes a givenTransactionCallback
without an actual transaction.Useful for testing: The behavior is equivalent to running with a transaction manager with no actual transaction (PROPAGATION_SUPPORTS) and no synchronization (SYNCHRONIZATION_NEVER).
For a
TransactionOperations
implementation with actual transaction processing, useTransactionTemplate
with an appropriatePlatformTransactionManager
.