public class LongTransactions extends Object
Transactions, the methods here don't require the operations to be chained together but can be
interleaved with non-transactional operations.
As with the methods from Transactions the atomicity provided here is of the weak type.
The methods in this class may be used in three different ways.
The simplest form is using the
longTransaction(RunnableWithException) to wrap individual transactional operations in a transaction.
The operations themselves must be defined using the transactionally(FunctionalTransaction) method. The main
body of each operation will be performed immediately, but the commit and rollback parts will be performed only when
the long transaction is comitted/rolled back.
Another possibility is to manually control the transaction using beginTx(), commitTx() and rollbackTx().
These methods make it easier to access variables defined during the transaction after the transaction finishes.
Notice that these methods are bound to the current thread, so if you use multithreading (e.g. parallel streams)
within the transaction, it will not work. Transactional operations are again defined using the
transactionally(FunctionalTransaction) method.
The third way to control a transaction is to use the LongTransaction object returned by beginTx().
This object has the LongTransaction.commit() and LongTransaction.rollback() methods. The transactional
operations are added using the LongTransaction.transactionally(FunctionalTransaction) method. This way of explicitly
referencing the transaction allows multithreaded code to be run within a transaction. Notice that LongTransaction
implements AutoCloseable which means that the transaction will be committed unless a rollback has been called
before.Transactions| Constructor and Description |
|---|
LongTransactions() |
| Modifier and Type | Method and Description |
|---|---|
static me.jaksa.LongTransaction |
beginTx()
Starts a
LongTransaction and binds it to the current thread. |
static void |
commitTx()
Commits the
LongTransaction bound to the current thread. |
static void |
longTransaction(RunnableWithException r)
Defines the transaction boundaries.
|
static void |
rollbackTx()
Rolls back the
LongTransaction bound to the current thread. |
static <T> T |
transactionally(FunctionalTransaction<T> operation)
Adds a transactional operation the the transaction bound to the current thread.
|
public static void longTransaction(RunnableWithException r)
transactionally(FunctionalTransaction).r - the runnable to execute in a transactionpublic static me.jaksa.LongTransaction beginTx()
LongTransaction and binds it to the current thread.LongTransactionpublic static void commitTx()
LongTransaction bound to the current thread. All transactional operations defined with
transactionally(FunctionalTransaction) will be committed.public static void rollbackTx()
LongTransaction bound to the current thread. All transactional operations defined with
transactionally(FunctionalTransaction) will be rolled back.public static <T> T transactionally(FunctionalTransaction<T> operation)
LongTransaction is committed/rolled back.T - operation - Copyright © 2019. All rights reserved.