Interface QuarkusTransaction
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:
{
BeginOptions.timeout(int)/TransactionRunnerOptions.timeout(int)can be used to set the new transactions timeout, without affecting the per thread default. - Lambda Style Transactions:
RunnableandCallableinstances 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
Modifier and TypeMethodDescriptionstatic voidbegin()Starts a transaction, using the system default timeout.static voidbegin(BeginOptions options) Starts a transaction, using the system default timeout.static BeginOptionsstatic <T> Tcall(RunOptions options, Callable<T> task) Deprecated.static <T> TDeprecated.For the same semantics, use.QuarkusTransaction.requiringNew().call(task)static voidcommit()Commits the current transaction.static TransactionRunnerOptionsStarts the definition of a transaction runner, which can then be used to run a task (Runnable,Callable, ...), withTransactionSemantics.DISALLOW_EXISTINGsemantics: If a transaction is already associated with the current thread aQuarkusTransactionExceptionwill be thrown, Otherwise a new transaction is started, and follows all the normal lifecycle rules.static intReturns the status of the current transaction.static booleanisActive()If a transaction is active.static booleanIf the transaction is rollback onlystatic TransactionRunnerOptionsStarts the definition of a transaction runner, which can then be used to run a task (Runnable,Callable, ...), withTransactionSemantics.JOIN_EXISTINGsemantics: If no transaction is active then a new transaction will be started, and committed when the method ends.static TransactionRunnerOptionsStarts the definition of a transaction runner, which can then be used to run a task (Runnable,Callable, ...), withTransactionSemantics.REQUIRE_NEWsemantics: If an existing transaction is already associated with the current thread then the transaction is suspended, then a new transaction is started which follows all the normal lifecycle rules, and when it's complete the original transaction is resumed.static voidrollback()Rolls back the current transaction.static voidrun(RunOptions options, Runnable task) Deprecated.static voidDeprecated.For the same semantics, use.QuarkusTransaction.requiringNew().run(task)static TransactionRunnerOptionsrunner(TransactionSemantics semantics) Starts the definition of a transaction runner, which can then be used to run a task (Runnable,Callable, ...), following the selectedTransactionSemantics.static RunOptionsDeprecated.static voidMarks the transaction as rollback only.static TransactionRunnerOptionsStarts the definition of a transaction runner, which can then be used to run a task (Runnable,Callable, ...), withTransactionSemantics.SUSPEND_EXISTINGsemantics: If no transaction is active then these semantics are basically a no-op.
-
Method Details
-
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
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:
trueif the transaction is active.
-
getStatus
static int getStatus()Returns the status of the current transaction.- Returns:
- The status of the current transaction based on the
Statusconstants.
-
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. -
joiningExisting
Starts the definition of a transaction runner, which can then be used to run a task (Runnable,Callable, ...), withTransactionSemantics.JOIN_EXISTINGsemantics:- If no transaction is active then a new transaction will be started, and committed when the method ends.
- If an exception is thrown the exception handler registered by
TransactionRunnerOptions.exceptionHandler(Function)will be called to decide if the TX should be committed or rolled back. - If an existing transaction is active then the method is run in the context of the existing transaction. If an
exception is thrown the exception handler will be called, however
a result of
TransactionExceptionResult.ROLLBACKwill result in the TX marked as rollback only, while a result ofTransactionExceptionResult.COMMITwill result in no action being taken.
Examples of use:
QuarkusTransaction.joiningExisting().run(() -> ...); int value = QuarkusTransaction.joiningExisting().call(() -> { ...; return 42; }); -
requiringNew
Starts the definition of a transaction runner, which can then be used to run a task (Runnable,Callable, ...), withTransactionSemantics.REQUIRE_NEWsemantics:- If an existing transaction is already associated with the current thread then the transaction is suspended, then a new transaction is started which follows all the normal lifecycle rules, and when it's complete the original transaction is resumed.
- Otherwise a new transaction is started, and follows all the normal lifecycle rules.
Examples of use:
QuarkusTransaction.requiringNew().run(() -> ...); int value = QuarkusTransaction.requiringNew().call(() -> { ...; return 42; }); -
disallowingExisting
Starts the definition of a transaction runner, which can then be used to run a task (Runnable,Callable, ...), withTransactionSemantics.DISALLOW_EXISTINGsemantics:- If a transaction is already associated with the current thread a
QuarkusTransactionExceptionwill be thrown, - Otherwise a new transaction is started, and follows all the normal lifecycle rules.
Examples of use:
QuarkusTransaction.requiringNew().run(() -> ...); int value = QuarkusTransaction.requiringNew().call(() -> { ...; return 42; }); - If a transaction is already associated with the current thread a
-
suspendingExisting
Starts the definition of a transaction runner, which can then be used to run a task (Runnable,Callable, ...), withTransactionSemantics.SUSPEND_EXISTINGsemantics:- If no transaction is active then these semantics are basically a no-op.
- If a transaction is active then it is suspended, and resumed after the task is run.
- The exception handler will never be consulted when these semantics are in use, specifying both an exception handler and these semantics are considered an error.
- These semantics allows for code to easily be run outside the scope of a transaction.
Examples of use:
QuarkusTransaction.requiringNew().run(() -> ...); int value = QuarkusTransaction.requiringNew().call(() -> { ...; return 42; }); -
runner
Starts the definition of a transaction runner, which can then be used to run a task (Runnable,Callable, ...), following the selectedTransactionSemantics.Examples of use:
QuarkusTransaction.runner(TransactionSemantics.REQUIRE_NEW).run(() -> ...); QuarkusTransaction.runner(TransactionSemantics.JOIN_EXISTING).run(() -> ...); QuarkusTransaction.runner(TransactionSemantics.SUSPEND_EXISTING).run(() -> ...); QuarkusTransaction.runner(TransactionSemantics.DISALLOW_EXISTING).run(() -> ...); int value = QuarkusTransaction.runner(TransactionSemantics.REQUIRE_NEW).call(() -> { ...; return 42; }); int value = QuarkusTransaction.runner(TransactionSemantics.JOIN_EXISTING).call(() -> { ...; return 42; }); int value = QuarkusTransaction.runner(TransactionSemantics.SUSPEND_EXISTING).call(() -> { ...; return 42; }); int value = QuarkusTransaction.runner(TransactionSemantics.DISALLOW_EXISTING).call(() -> { ...; return 42; });- Parameters:
semantics- The selectedTransactionSemantics.- Returns:
- An interface that allow various options of a transaction runner to be customized,
or a
Runnable/Callableto be executed. - See Also:
-
run
Deprecated.For the same semantics, use.QuarkusTransaction.requiringNew().run(task)joiningExisting(),disallowingExisting(),suspendingExisting()andrunner(TransactionSemantics)can also be used for alternate semantics and options.Runs a task in a new transaction with the default timeout. This defaults toTransactional.TxType.REQUIRES_NEWsemantics, however alternate semantics can be requested usingrun(RunOptions, Runnable).- Parameters:
task- The task to run in a transaction
-
run
Deprecated.Runs a task in a new transaction with the default timeout. This defaults toTransactional.TxType.REQUIRES_NEWsemantics, however alternate semantics can be specified using theoptionsparameter.- Parameters:
options- Options that apply to the new transactiontask- The task to run in a transaction
-
call
Deprecated.For the same semantics, use.QuarkusTransaction.requiringNew().call(task)joiningExisting(),disallowingExisting(),suspendingExisting()andrunner(TransactionSemantics)can also be used for alternate semantics and options.Calls a task in a new transaction with the default timeout. This defaults toTransactional.TxType.REQUIRES_NEWsemantics, however alternate semantics can be requested usingcall(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
Deprecated.Calls a task in a new transaction with the default timeout. This defaults toTransactional.TxType.REQUIRES_NEWsemantics, however alternate semantics can be requested using theoptionsparameter.If the task throws a checked exception it will be wrapped with a
QuarkusTransactionException- Parameters:
options- Options that apply to the new transactiontask- The task to run in a transaction
-
runOptions
Deprecated.- Returns:
- a new RunOptions
-
beginOptions
- Returns:
- a new BeginOptions
-
requiringNew(),joiningExisting(),disallowingExisting(),suspendingExisting()orrunner(TransactionSemantics)instead.