Class Transaction

java.lang.Object
com.pyranid.Transaction

@ThreadSafe public final class Transaction extends Object
Represents a database transaction.

Note that commit and rollback operations are controlled internally by Database.

Since:
1.0.0
Author:
Mark Allen
  • Method Details

    • toString

      public @NonNull String toString()
      Overrides:
      toString in class Object
    • createSavepoint

      public @NonNull Savepoint createSavepoint()
      Creates a transaction savepoint that can be rolled back to via rollback(Savepoint).

      For most application code, prefer withSavepoint(TransactionalOperation) or withSavepoint(ReturningTransactionalOperation) so rollback and release cleanup are handled automatically.

      Returns:
      a transaction savepoint
      Throws:
      IllegalStateException - if this transaction has already completed
    • rollback

      public void rollback(@NonNull Savepoint savepoint)
      Rolls back to the provided transaction savepoint.
      Parameters:
      savepoint - the savepoint to roll back to
      Throws:
      IllegalStateException - if this transaction has already completed
    • releaseSavepoint

      public void releaseSavepoint(@NonNull Savepoint savepoint)
      Releases the provided transaction savepoint.

      For most application code, prefer withSavepoint(TransactionalOperation) or withSavepoint(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

      public void withSavepoint(@NonNull TransactionalOperation transactionalOperation)
      Performs an operation inside a transaction savepoint.

      If transactionalOperation completes 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 transactionalOperation completes 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

      public @NonNull Boolean isRollbackOnly()
      Should this transaction be rolled back upon completion?

      Default value is false.

      Returns:
      true if this transaction should be rolled back, false otherwise
    • setRollbackOnly

      public void setRollbackOnly(@NonNull Boolean rollbackOnly)
      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:
      true if the post-transaction operation was removed, false otherwise
    • getPostTransactionOperations

      Gets an unmodifiable list of post-transaction operations.

      To manipulate the list, use addPostTransactionOperation(Consumer) and removePostTransactionOperation(Consumer).

      Returns:
      the list of post-transaction operations
    • getTransactionIsolation

      Get the isolation level for this transaction.
      Returns:
      the isolation level