Interface QuarkusTransaction


  • public interface QuarkusTransaction
    A simplified transaction interface. While broadly covering the same use cases as UserTransaction, this class is designed to be easier to use. The main features it offers over UserTransaction are:
    • No Checked Exceptions: All underlying checked exceptions are wrapped in an unchecked QuarkusTransactionException.
    • No Transaction Leaks: Transactions are tied to the request scope, if the scope is destroyed before the transaction is committed the transaction is rolled back. Note that this means this can only currently be used when the request scope is active.
    • Per Transaction Timeouts: RunOptions.timeout(int) can be used to set the new transactions timeout, without affecting the per thread default.
    • Lambda Style Transactions: Runnable and Callable instances can be run inside the scope of a new transaction.

    Note that any checked exception will be wrapped by a QuarkusTransactionException, while unchecked exceptions are allowed to propagate unchanged.

    • Method Summary

      Static Methods 
      Modifier and Type Method Description
      static void begin()
      Starts a transaction, using the system default timeout.
      static void begin​(BeginOptions options)
      Starts a transaction, using the system default timeout.
      static BeginOptions beginOptions()  
      static <T> T call​(RunOptions options, Callable<T> task)
      Calls a task in a new transaction with the default timeout.
      static <T> T call​(Callable<T> task)
      Calls a task in a new transaction with the default timeout.
      static void commit()
      Commits the current transaction.
      static boolean isActive()
      If a transaction is active.
      static boolean isRollbackOnly()
      If the transaction is rollback only
      static void rollback()
      Rolls back the current transaction.
      static void run​(RunOptions options, Runnable task)
      Runs a task in a new transaction with the default timeout.
      static void run​(Runnable task)
      Runs a task in a new transaction with the default timeout.
      static RunOptions runOptions()  
      static void setRollbackOnly()
      Marks the transaction as rollback only.
    • Method Detail

      • begin

        static void begin()
        Starts a transaction, using the system default timeout.

        This transaction will be tied to the current request scope, if it is not committed when the scope is destroyed then it will be rolled back to prevent transaction leaks.

      • begin

        static void begin​(BeginOptions options)
        Starts a transaction, using the system default timeout.

        This transaction will be tied to the current request scope, if it is not committed when the scope is destroyed then it will be rolled back to prevent transaction leaks.

        Parameters:
        options - Options that apply to the new transaction
      • commit

        static void commit()
        Commits the current transaction.
      • rollback

        static void rollback()
        Rolls back the current transaction.
      • isActive

        static boolean isActive()
        If a transaction is active.
        Returns:
        true if the transaction is active.
      • isRollbackOnly

        static boolean isRollbackOnly()
        If the transaction is rollback only
        Returns:
        If the transaction has been marked for rollback
      • setRollbackOnly

        static void setRollbackOnly()
        Marks the transaction as rollback only. Operations can still be carried out, however the transaction cannot be successfully committed.
      • run

        static void run​(Runnable task)
        Runs a task in a new transaction with the default timeout. This defaults to Transactional.TxType.REQUIRES_NEW semantics, however alternate semantics can be requested using run(RunOptions, Runnable).
        Parameters:
        task - The task to run in a transaction
      • run

        static void run​(RunOptions options,
                        Runnable task)
        Runs a task in a new transaction with the default timeout. This defaults to Transactional.TxType.REQUIRES_NEW semantics, however alternate semantics can be specified using the options parameter.
        Parameters:
        options - Options that apply to the new transaction
        task - The task to run in a transaction
      • call

        static <T> T call​(Callable<T> task)
        Calls a task in a new transaction with the default timeout. This defaults to Transactional.TxType.REQUIRES_NEW semantics, however alternate semantics can be requested using call(RunOptions, Callable).

        If the task throws a checked exception it will be wrapped with a QuarkusTransactionException

        Parameters:
        task - The task to run in a transaction
      • call

        static <T> T call​(RunOptions options,
                          Callable<T> task)
        Calls a task in a new transaction with the default timeout. This defaults to Transactional.TxType.REQUIRES_NEW semantics, however alternate semantics can be requested using call(RunOptions, Callable).

        If the task throws a checked exception it will be wrapped with a QuarkusTransactionException

        Parameters:
        task - The task to run in a transaction
      • runOptions

        static RunOptions runOptions()
        Returns:
        a new RunOptions
      • beginOptions

        static BeginOptions beginOptions()
        Returns:
        a new BeginOptions