Package com.vaadin.signals.impl
Class Transaction
java.lang.Object
com.vaadin.signals.impl.Transaction
- Direct Known Subclasses:
StagedTransaction
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 enumThe type of a transaction, determining how it handles reads and writes. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidcommit(Consumer<SignalOperation.ResultOrError<Void>> resultHandler) Commits any staged commands in this transaction.static TransactionGets the current transaction handler.voidinclude(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 voidinclude(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 booleanChecks whether a transaction is currently active on the current thread.abstract TreeRevisionread(SignalTree tree) Gets a revision for reading from the given tree in the context of this transaction.protected abstract voidrollback()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 voidRuns the given task outside any transaction.static <T> TrunWithoutTransaction(Supplier<T> task) Runs the given supplier outside any transaction and returns the supplied value.
-
Constructor Details
-
Transaction
public Transaction()Creates a new 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:
trueif 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 fromgetCurrent().The transaction will be committed after running the task, or rolled back if the task throws an exception.
- Type Parameters:
T- the supplier type- Parameters:
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 fromgetCurrent().- Type Parameters:
T- the supplier type- Parameters:
transactionTask- the supplier to run in a transaction, notnulltransactionType- 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 fromgetCurrent().The transaction will be committed after running the task, or rolled back if the task throws an exception.
- Parameters:
transactionTask- the task to run, notnulltransactionType- 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 fromgetCurrent().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 type- Parameters:
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 anIllegalStateException.- Parameters:
tree- the signal tree against which to run the command, notnullcommand- the command to include, notnullresultHandler- the handler of the command result, ornullto ignore the resultapplyToTree-trueto apply the command to the underlying tree,falseto 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 anIllegalStateException.- Parameters:
tree- the signal tree against which to run the command, notnullcommand- the command to apply, notnullresultHandler- the handler of the command result, ornullto 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.
-