Top Description Methods
org.springframework.transaction.support

public Interface TransactionOperations

Known Direct Implementers
org.springframework.transaction.support.TransactionTemplate, org.springframework.transaction.support.WithoutTransactionOperations
Imports
java.util.function.Consumer, org.springframework.lang.Nullable, org.springframework.transaction.TransactionException, .TransactionStatus

Interface specifying basic transaction execution operations. Implemented by TransactionTemplate. Not often used directly, but a useful option to enhance testability, as it can easily be mocked or stubbed.
Author
Juergen Hoeller
Since
2.0.4

Method Summary

Modifier and TypeMethod and Description
public <T> T

Returns:

a result object returned by the callback, or null if none
execute
(TransactionCallback<T>
the callback object that specifies the transactional action
action
)

Execute the action specified by the given callback object within a transaction.

public default void
executeWithoutResult(Consumer<TransactionStatus>
the Runnable that specifies the transactional action
action
)

Execute the action specified by the given Runnable within a transaction.

public static TransactionOperations
withoutTransaction()

Return an implementation of the TransactionOperations interface which executes a given TransactionCallback without an actual transaction.

Method Detail

executeback to summary
public <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.

Parameters
action:TransactionCallback<T>

the callback object that specifies the transactional action

Returns:T

a result object returned by the callback, or null if none

Annotations
@Nullable
Exceptions
TransactionException:
in case of initialization, rollback, or system errors
RuntimeException:
if thrown by the TransactionCallback
See Also
executeWithoutResult(Consumer)
executeWithoutResultback to summary
public default void executeWithoutResult(Consumer<TransactionStatus> action) throws TransactionException

Execute the action specified by the given Runnable within a transaction.

If you need to return an object from the callback or access the org.springframework.transaction.TransactionStatus from within the callback, use execute(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.

Parameters
action:Consumer<TransactionStatus>

the Runnable that specifies the transactional action

Exceptions
TransactionException:
in case of initialization, rollback, or system errors
RuntimeException:
if thrown by the Runnable
Since
5.2
See Also
execute(TransactionCallback), TransactionCallbackWithoutResult
withoutTransactionback to summary
public static TransactionOperations withoutTransaction()

Return an implementation of the TransactionOperations interface which executes a given TransactionCallback 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, use TransactionTemplate with an appropriate org.springframework.transaction.PlatformTransactionManager.

Since
5.2
See Also
org.springframework.transaction.TransactionDefinition#PROPAGATION_SUPPORTS, AbstractPlatformTransactionManager#SYNCHRONIZATION_NEVER, TransactionTemplate