com.vaadin.signals.impl.
Class Transaction
Direct Known Subclasses:
A context for running commands that might be related to each other. The
current transaction is registered as a ThreadLocal
that is used by
all signal operations running on that thread. Transactions can be nested so
that changes from an inner transaction are rolled up to the outer
transaction.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
The type of a transaction, determining how it handles reads and writes.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract void
commit
(Consumer<SignalOperation.ResultOrError<Void>> resultHandler) Commits any staged commands in this transaction.
static Transaction
Gets the current transaction handler.
void
include
(SignalTree tree, SignalCommand command, Consumer<CommandResult> resultHandler) Includes the given command to the given tree in the context of this transaction and sets the command to be applied to the underlying signal tree.
protected abstract void
include
(SignalTree tree, SignalCommand command, Consumer<CommandResult> resultHandler, boolean applyToTree) Includes the given command to the given tree in the context of this transaction and optionally also sets the command to be applied to the underlying signal tree.
static boolean
Checks whether a transaction is currently active on the current thread.
abstract TreeRevision
read
(SignalTree tree) Gets a revision for reading from the given tree in the context of this transaction.
protected abstract boolean
readonly()
Checks whether this is a read-only transaction.
protected abstract void
rollback()
Rolls back any staged commands in this transaction and notifies the result handlers for those commands.
static TransactionOperation<Void>
runInTransaction
(Runnable transactionTask) Runs the given task in a regular transaction and returns an operation object without a value.
static TransactionOperation<Void>
runInTransaction
(Runnable transactionTask, Transaction.Type transactionType) Runs the given task in a transaction of the given type and returns an operation object without a value.
static <T> TransactionOperation<T>
runInTransaction
(Supplier<T> transactionTask) Runs the given supplier in a regular transaction and returns an operation object that wraps the supplier value.
static <T> TransactionOperation<T>
runInTransaction
(Supplier<T> transactionTask, Transaction.Type transactionType) Runs the given supplier in a transaction of the given type and returns an operation object that wraps the supplier value.
static void
Runs the given task outside any transaction.
static <T> T
runWithoutTransaction
(Supplier<T> task) Runs the given supplier outside any transaction and returns the supplied value.
-
Constructor Details
-
Transaction
public Transaction()
-
-
Method Details
-
getCurrent
Gets the current transaction handler.
Returns:
the current transaction handler, not
null
-
inTransaction
public static boolean inTransaction()Checks whether a transaction is currently active on the current thread.
Returns:
true
if a transaction is active -
runInTransaction
Runs the given supplier in a regular transaction and returns an operation object that wraps the supplier value. The created transaction handler will be available from
getCurrent()
.The transaction will be committed after running the task, or rolled back if the task throws an exception.
Type Parameters:
T
- the supplier typeParameters:
transactionTask
- the supplier to run in a transaction, notnull
Returns:
the operation object that wraps the supplier value, not
null
-
runInTransaction
public static <T> TransactionOperation<T> runInTransaction(Supplier<T> transactionTask, Transaction.Type transactionType) Runs the given supplier in a transaction of the given type and returns an operation object that wraps the supplier value. The created transaction handler will be available from
getCurrent()
.Type Parameters:
T
- the supplier typeParameters:
transactionTask
- the supplier to run in a transaction, notnull
transactionType
- the type of the transaction, notnull
Returns:
the operation object that wraps the supplier value, not
null
-
runInTransaction
public static TransactionOperation<Void> runInTransaction(Runnable transactionTask, Transaction.Type transactionType) Runs the given task in a transaction of the given type and returns an operation object without a value. The created transaction handler will be available from
getCurrent()
.The transaction will be committed after running the task, or rolled back if the task throws an exception.
Parameters:
transactionTask
- the task to run, notnull
transactionType
- the type of the transaction, notnull
Returns:
the operation object, not
null
-
runInTransaction
Runs the given task in a regular transaction and returns an operation object without a value. The created transaction handler will be available from
getCurrent()
.The transaction will be committed after running the task, or rolled back if the task throws an exception.
Parameters:
transactionTask
- the task to run, notnull
Returns:
the operation object, not
null
-
runWithoutTransaction
Runs the given supplier outside any transaction and returns the supplied value. The current transaction will be restored after the task has been run.
Type Parameters:
T
- the supplier typeParameters:
task
- the supplier to run, notnull
Returns:
the value returned from the supplier
-
runWithoutTransaction
Runs the given task outside any transaction. The current transaction will be restored after the task has been run.
Parameters:
task
- the task to run, notnull
-
include
protected abstract void include(SignalTree tree, SignalCommand command, Consumer<CommandResult> resultHandler, boolean applyToTree) Includes the given command to the given tree in the context of this transaction and optionally also sets the command to be applied to the underlying signal tree. Depending on the transaction type, an applied command may be applied immediately, collected to be applied upon committing, or rejected with an
IllegalStateException
.Parameters:
tree
- the signal tree against which to run the command, notnull
command
- the command to include, notnull
resultHandler
- the handler of the command result, ornull
to ignore the resultapplyToTree
-true
to apply the command to the underlying tree,false
to only update the transaction's repeatable-read revision -
include
Includes the given command to the given tree in the context of this transaction and sets the command to be applied to the underlying signal tree. Depending on the transaction type, an applied command may be applied immediately, collected to be applied upon committing, or rejected with an
IllegalStateException
.Parameters:
tree
- the signal tree against which to run the command, notnull
command
- the command to apply, notnull
resultHandler
- the handler of the command result, ornull
to ignore the result -
read
Gets a revision for reading from the given tree in the context of this transaction.
Parameters:
tree
- the tree to read from, notnull
Returns:
a tree revision to read from, not
null
-
commit
Commits any staged commands in this transaction.
Parameters:
resultHandler
- a consumer to update the result value in the corresponding transaction operation, notnull
-
rollback
protected abstract void rollback()Rolls back any staged commands in this transaction and notifies the result handlers for those commands.
-
readonly
protected abstract boolean readonly()Checks whether this is a read-only transaction. A read-only transaction only allows writes to computed signal trees since those are not user-originated write operations but just side effects of lazy evaluation.
Returns:
false
if this transaction allows regular writes to the underlying tree,true
otherwise.
-