Interface SynchronousTransactionState
-
- All Superinterfaces:
TransactionState
- All Known Implementing Classes:
DefaultSynchronousTransactionState
@Internal public interface SynchronousTransactionState extends TransactionState
The synchronous transaction state. Extracted fromTransactionSynchronizationManager
to allow not thread-local state.- Since:
- 3.4.0
- Author:
- Denis Stepanov
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
clear()
Clear the entire transaction synchronization state: registered synchronizations as well as the various transaction characteristics.void
clearSynchronization()
Deactivate transaction synchronization for the current state.java.util.List<TransactionSynchronization>
getSynchronizations()
Return an unmodifiable snapshot list of all registered synchronizations for the current state.TransactionDefinition.Isolation
getTransactionIsolationLevel()
Return the isolation level for the current transaction, if any.java.lang.String
getTransactionName()
Return the name of the current transaction, ornull
if none set.void
initSynchronization()
Activate transaction synchronization for the current state.boolean
isActualTransactionActive()
Return whether there currently is an actual transaction active.boolean
isSynchronizationActive()
Return if transaction synchronization is active for the current state.boolean
isTransactionReadOnly()
Return whether the current transaction is marked as read-only.void
registerSynchronization(TransactionSynchronization synchronization)
Register a new transaction synchronization for the current state.void
setActualTransactionActive(boolean active)
Expose whether there currently is an actual transaction active.void
setTransactionIsolationLevel(TransactionDefinition.Isolation isolationLevel)
Expose an isolation level for the current transaction.void
setTransactionName(java.lang.String name)
Expose the name of the current transaction, if any.void
setTransactionReadOnly(boolean readOnly)
Expose a read-only flag for the current transaction.
-
-
-
Method Detail
-
isSynchronizationActive
boolean isSynchronizationActive()
Return if transaction synchronization is active for the current state. Can be called before register to avoid unnecessary instance creation.- Returns:
- True if a synchronization is active
- See Also:
registerSynchronization(io.micronaut.transaction.support.TransactionSynchronization)
-
initSynchronization
void initSynchronization() throws java.lang.IllegalStateException
Activate transaction synchronization for the current state. Called by a transaction manager on transaction begin.- Throws:
java.lang.IllegalStateException
- if synchronization is already active
-
registerSynchronization
void registerSynchronization(@NonNull TransactionSynchronization synchronization)
Register a new transaction synchronization for the current state. Typically called by resource management code.Note that synchronizations can implement the
Ordered
interface. They will be executed in an order according to their order value (if any).- Parameters:
synchronization
- the synchronization object to register- Throws:
java.lang.IllegalStateException
- if transaction synchronization is not active- See Also:
Ordered
-
getSynchronizations
@NonNull java.util.List<TransactionSynchronization> getSynchronizations() throws java.lang.IllegalStateException
Return an unmodifiable snapshot list of all registered synchronizations for the current state.- Returns:
- unmodifiable List of TransactionSynchronization instances
- Throws:
java.lang.IllegalStateException
- if synchronization is not active- See Also:
TransactionSynchronization
-
clearSynchronization
void clearSynchronization() throws java.lang.IllegalStateException
Deactivate transaction synchronization for the current state. Called by the transaction manager on transaction cleanup.- Throws:
java.lang.IllegalStateException
- if synchronization is not active
-
setTransactionName
void setTransactionName(@Nullable java.lang.String name)
Expose the name of the current transaction, if any. Called by the transaction manager on transaction begin and on cleanup.- Parameters:
name
- the name of the transaction, ornull
to reset it- See Also:
TransactionDefinition.getName()
-
getTransactionName
@Nullable java.lang.String getTransactionName()
Return the name of the current transaction, ornull
if none set. To be called by resource management code for optimizations per use case, for example to optimize fetch strategies for specific named transactions.- Returns:
- The current transaction name
- See Also:
TransactionDefinition.getName()
-
setTransactionReadOnly
void setTransactionReadOnly(boolean readOnly)
Expose a read-only flag for the current transaction. Called by the transaction manager on transaction begin and on cleanup.- Parameters:
readOnly
-true
to mark the current transaction as read-only;false
to reset such a read-only marker- See Also:
TransactionDefinition.isReadOnly()
-
isTransactionReadOnly
boolean isTransactionReadOnly()
Return whether the current transaction is marked as read-only. To be called by resource management code when preparing a newly created resource (for example, a Hibernate Session).Note that transaction synchronizations receive the read-only flag as argument for the
beforeCommit
callback, to be able to suppress change detection on commit. The present method is meant to be used for earlier read-only checks, for example to set the flush mode of a Hibernate Session to "FlushMode.NEVER" upfront.- Returns:
- Whether the transaction is read only
- See Also:
TransactionDefinition.isReadOnly()
,TransactionSynchronization.beforeCommit(boolean)
-
setTransactionIsolationLevel
void setTransactionIsolationLevel(@Nullable TransactionDefinition.Isolation isolationLevel)
Expose an isolation level for the current transaction. Called by the transaction manager on transaction begin and on cleanup.- Parameters:
isolationLevel
- the isolation level to expose, according to the JDBC Connection constants (equivalent to the corresponding TransactionDefinition constants), ornull
to reset it- See Also:
Connection.TRANSACTION_READ_UNCOMMITTED
,Connection.TRANSACTION_READ_COMMITTED
,Connection.TRANSACTION_REPEATABLE_READ
,Connection.TRANSACTION_SERIALIZABLE
,TransactionDefinition.Isolation.READ_UNCOMMITTED
,TransactionDefinition.Isolation.READ_COMMITTED
,TransactionDefinition.Isolation.REPEATABLE_READ
,TransactionDefinition.Isolation.SERIALIZABLE
,TransactionDefinition.getIsolationLevel()
-
getTransactionIsolationLevel
@Nullable TransactionDefinition.Isolation getTransactionIsolationLevel()
Return the isolation level for the current transaction, if any. To be called by resource management code when preparing a newly created resource (for example, a JDBC Connection).- Returns:
- the currently exposed isolation level, according to the
JDBC Connection constants (equivalent to the corresponding
TransactionDefinition constants), or
null
if none - See Also:
Connection.TRANSACTION_READ_UNCOMMITTED
,Connection.TRANSACTION_READ_COMMITTED
,Connection.TRANSACTION_REPEATABLE_READ
,Connection.TRANSACTION_SERIALIZABLE
,TransactionDefinition.Isolation.READ_UNCOMMITTED
,TransactionDefinition.Isolation.READ_COMMITTED
,TransactionDefinition.Isolation.REPEATABLE_READ
,TransactionDefinition.Isolation.SERIALIZABLE
,TransactionDefinition.getIsolationLevel()
-
setActualTransactionActive
void setActualTransactionActive(boolean active)
Expose whether there currently is an actual transaction active. Called by the transaction manager on transaction begin and on cleanup.- Parameters:
active
-true
to mark the current state as being associated with an actual transaction;false
to reset that marker
-
isActualTransactionActive
boolean isActualTransactionActive()
Return whether there currently is an actual transaction active. This indicates whether the current state is associated with an actual transaction rather than just with active transaction synchronization.To be called by resource management code that wants to discriminate between active transaction synchronization (with or without backing resource transaction; also on PROPAGATION_SUPPORTS) and an actual transaction being active (with backing resource transaction; on PROPAGATION_REQUIRED, PROPAGATION_REQUIRES_NEW, etc).
- Returns:
- Whether a transaction is active
- See Also:
isSynchronizationActive()
-
clear
void clear()
Clear the entire transaction synchronization state: registered synchronizations as well as the various transaction characteristics.
-
-