Class Transaction
Note that commit and rollback operations are controlled internally by Database.
- Since:
- 1.0.0
- Author:
- Mark Allen
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddPostTransactionOperation(@NonNull Consumer<TransactionResult> postTransactionOperation) Adds an operation to the list of operations to be executed when the transaction completes.@NonNull SavepointCreates a transaction savepoint that can be rolled back to viarollback(Savepoint).@NonNull List<@NonNull Consumer<TransactionResult>> Gets an unmodifiable list of post-transaction operations.@NonNull TransactionIsolationGet the isolation level for this transaction.@NonNull BooleanShould this transaction be rolled back upon completion?voidreleaseSavepoint(@NonNull Savepoint savepoint) Releases the provided transaction savepoint.@NonNull BooleanremovePostTransactionOperation(@NonNull Consumer<TransactionResult> postTransactionOperation) Removes an operation from the list of operations to be executed when the transaction completes.voidRolls back to the provided transaction savepoint.voidsetRollbackOnly(@NonNull Boolean rollbackOnly) Sets whether this transaction should be rolled back upon completion.@NonNull StringtoString()<T> @NonNull Optional<T> withSavepoint(@NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation inside a transaction savepoint and optionally returns a value.voidwithSavepoint(@NonNull TransactionalOperation transactionalOperation) Performs an operation inside a transaction savepoint.
-
Method Details
-
toString
-
createSavepoint
Creates a transaction savepoint that can be rolled back to viarollback(Savepoint).For most application code, prefer
withSavepoint(TransactionalOperation)orwithSavepoint(ReturningTransactionalOperation)so rollback and release cleanup are handled automatically.- Returns:
- a transaction savepoint
- Throws:
IllegalStateException- if this transaction has already completed
-
rollback
Rolls back to the provided transaction savepoint.- Parameters:
savepoint- the savepoint to roll back to- Throws:
IllegalStateException- if this transaction has already completed
-
releaseSavepoint
Releases the provided transaction savepoint.For most application code, prefer
withSavepoint(TransactionalOperation)orwithSavepoint(ReturningTransactionalOperation)so rollback and release cleanup are handled automatically.- Parameters:
savepoint- the savepoint to release- Throws:
IllegalStateException- if this transaction has already completed- Since:
- 4.1.0
-
withSavepoint
Performs an operation inside a transaction savepoint.If
transactionalOperationcompletes successfully, the savepoint is released when the driver supports release. If an exception bubbles out, Pyranid rolls back to the savepoint, attempts to release it, and preserves cleanup failures as suppressed exceptions on the thrown exception.Nested savepoint usage should be stack-like: finish inner savepoints before manually releasing or rolling back outer savepoints.
- Parameters:
transactionalOperation- the operation to perform inside a savepoint- Throws:
IllegalStateException- if this transaction has already completed- Since:
- 4.1.0
-
withSavepoint
public <T> @NonNull Optional<T> withSavepoint(@NonNull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation inside a transaction savepoint and optionally returns a value.If
transactionalOperationcompletes successfully, the savepoint is released when the driver supports release. If an exception bubbles out, Pyranid rolls back to the savepoint, attempts to release it, and preserves cleanup failures as suppressed exceptions on the thrown exception.Nested savepoint usage should be stack-like: finish inner savepoints before manually releasing or rolling back outer savepoints.
- Type Parameters:
T- the type to be returned- Parameters:
transactionalOperation- the operation to perform inside a savepoint- Returns:
- the result of the operation
- Throws:
IllegalStateException- if this transaction has already completed- Since:
- 4.1.0
-
isRollbackOnly
Should this transaction be rolled back upon completion?Default value is
false.- Returns:
trueif this transaction should be rolled back,falseotherwise
-
setRollbackOnly
Sets whether this transaction should be rolled back upon completion.- Parameters:
rollbackOnly- whether to set this transaction to be rollback-only
-
addPostTransactionOperation
public void addPostTransactionOperation(@NonNull Consumer<TransactionResult> postTransactionOperation) Adds an operation to the list of operations to be executed when the transaction completes.- Parameters:
postTransactionOperation- the post-transaction operation to add
-
removePostTransactionOperation
public @NonNull Boolean removePostTransactionOperation(@NonNull Consumer<TransactionResult> postTransactionOperation) Removes an operation from the list of operations to be executed when the transaction completes.- Parameters:
postTransactionOperation- the post-transaction operation to remove- Returns:
trueif the post-transaction operation was removed,falseotherwise
-
getPostTransactionOperations
Gets an unmodifiable list of post-transaction operations.To manipulate the list, use
addPostTransactionOperation(Consumer)andremovePostTransactionOperation(Consumer).- Returns:
- the list of post-transaction operations
-
getTransactionIsolation
Get the isolation level for this transaction.- Returns:
- the isolation level
-