Interface TransactionManager
- All Known Subinterfaces:
JdbcTransactionManager
- All Known Implementing Classes:
DataSourceTransactionManager,JsonTransactionManager
Implementations handle transaction boundaries (begin, commit, rollback) and provide connections that are aware of the current transaction context.
- Since:
- 6.2.0
- Author:
- wisdomme
-
Method Summary
Modifier and TypeMethodDescriptionvoidbegin()Begins a new transaction.voidcommit()Commits the current transaction.default ConnectionDeprecated, for removal: This API element is subject to removal in a future version.default LongReturns theSystem.nanoTime()value at which the active transaction's timeout budget expires, ornullwhen no timeout is configured for the active transaction -- no active transaction at all, or@Transactional(timeout=0)(the default).intGets the current transaction depth (for nested transactions).booleanChecks if there is an active transaction on the current thread.default voidRestores a transaction previously detached bysuspend().voidrollback()Rolls back the current transaction.default voidsetIsolationLevel(int level) Deprecated, for removal: This API element is subject to removal in a future version.JDBC-specific; program againstJdbcTransactionManager.setIsolationLevel(int)instead.default voidsetReadOnly(boolean readOnly) Deprecated, for removal: This API element is subject to removal in a future version.JDBC-specific; program againstJdbcTransactionManager.setReadOnly(boolean)instead.voidsetTimeout(int seconds) Sets the timeout for the current transaction.default Objectsuspend()Suspends the current transaction, if any, detaching it from this thread so an independent transaction can begin in its place.
-
Method Details
-
getConnection
Deprecated, for removal: This API element is subject to removal in a future version.JDBC-specific; not everyTransactionManagerbacks aDataSource. Program againstJdbcTransactionManager.getConnection()instead. This default throwsUnsupportedOperationExceptionfor any backend that only implements the base interface.Gets the current connection, creating a new one if necessary.If a transaction is active, returns the connection associated with it. Otherwise, returns a new connection from the data source.
- Returns:
- the current connection
- Since:
- 6.3.0 demoted from an abstract method to a
defaultone (D-04); zero binary removal. - Scheduled removal version:
- 6.4.0
-
begin
void begin()Begins a new transaction.Creates a new connection, disables auto-commit, and associates it with the current thread.
- Throws:
DataAccessException- if transaction cannot be started
-
commit
void commit()Commits the current transaction.Commits all changes made during the transaction and releases the connection.
- Throws:
DataAccessException- if commit fails
-
rollback
void rollback()Rolls back the current transaction.Discards all changes made during the transaction and releases the connection.
-
hasActiveTransaction
boolean hasActiveTransaction()Checks if there is an active transaction on the current thread.- Returns:
- true if a transaction is active
-
setIsolationLevel
Deprecated, for removal: This API element is subject to removal in a future version.JDBC-specific; program againstJdbcTransactionManager.setIsolationLevel(int)instead. This default throwsUnsupportedOperationExceptionfor any backend that only implements the base interface.Sets the isolation level for the current transaction.Must be called after begin() and before any statements are executed.
- Parameters:
level- the JDBC isolation level constant- Since:
- 6.3.0 demoted from an abstract method to a
defaultone (D-04); zero binary removal. - Scheduled removal version:
- 6.4.0
-
setReadOnly
Deprecated, for removal: This API element is subject to removal in a future version.JDBC-specific; program againstJdbcTransactionManager.setReadOnly(boolean)instead. This default throwsUnsupportedOperationExceptionfor any backend that only implements the base interface.Sets read-only mode for the current transaction.- Parameters:
readOnly- true for read-only mode- Since:
- 6.3.0 demoted from an abstract method to a
defaultone (D-04); zero binary removal. - Scheduled removal version:
- 6.4.0
-
setTimeout
void setTimeout(int seconds) Sets the timeout for the current transaction.Note: Not all databases/drivers support transaction timeout.
- Parameters:
seconds- the timeout in seconds
-
getTimeoutDeadlineNanos
Returns theSystem.nanoTime()value at which the active transaction's timeout budget expires, ornullwhen no timeout is configured for the active transaction -- no active transaction at all, or@Transactional(timeout=0)(the default).D-10: consulted per statement (by
TimeoutAwareQueryRunnerand the two directPreparedStatementcall sitesAbstractRelationalDataOperator.insertAll/updateAllbuild themselves) to compute the time remaining in the budget, not a value captured once -- the caller re-reads this on every statement.The default implementation always returns
null, meaning "never enforced" -- the same posture a backend whosesetTimeout(int)refuses any positive value outright (rather than ever recording a deadline) naturally has, since it never has anything to report here either. A backend that wants per-statement enforcement overrides this together withsetTimeout(int)-- overriding only one half of the pair leaves the other silently inert, the same cautionsuspend()'s javadoc gives for its pair.- Returns:
- the deadline, as a
System.nanoTime()value, ornullif none is active - Since:
- 6.3.0
-
getTransactionDepth
int getTransactionDepth()Gets the current transaction depth (for nested transactions).- Returns:
- the transaction depth (0 if no transaction)
-
suspend
Suspends the current transaction, if any, detaching it from this thread so an independent transaction can begin in its place. BacksPropagation.REQUIRES_NEWandPropagation.NOT_SUPPORTED(D-09).Deliberately typed to return an opaque
Objectrather than a backend-specific context type: a JDBC-backed manager suspends aConnection-holding context, a JSON snapshot manager (02-05) suspends a set of operator snapshots (D-03) - neither shape belongs on this base interface. Pass whatever this returns toresume(Object)to restore it; the caller must not otherwise inspect or reuse the returned value.The default implementation does not support suspension: it always returns
null, meaning nothing was suspended. A backend that maintains thread-bound transaction state (JDBC, JSON) must override this together withresume(Object)- overriding only one of the pair breaks the contract silently.- Returns:
- an opaque handle to resume later, or
nullif no transaction was active - Since:
- 6.3.0
-
resume
Restores a transaction previously detached bysuspend().nullis a no-op - it meanssuspend()found nothing active in the first place, so there is nothing to restore. Passing a handle thisTransactionManagerdid not itself produce is a programming error.The default implementation is a no-op, pairing with
suspend()'s default of always returningnull.- Parameters:
suspended- the handle returned by a priorsuspend()call, ornull- Since:
- 6.3.0
-
TransactionManagerbacks aDataSource.