Package com.vaadin.flow.signals.impl
Class Transaction
java.lang.Object
com.vaadin.flow.signals.impl.Transaction
- All Implemented Interfaces:
Serializable
- 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.- See Also:
-
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 TransactionCreates a new write-through transaction that provides repeatable reads while applying commands immediately to the underlying tree.static TransactionGets the current transaction handler.voidinclude(SignalTree tree, SignalCommand command, @Nullable CommandsAndHandlers.CommandResultHandler 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.abstract voidinclude(SignalTree tree, SignalCommand command, @Nullable CommandsAndHandlers.CommandResultHandler 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 an explicit transaction (started viarunInTransaction(com.vaadin.flow.signals.function.ValueSupplier<T>)) is currently active on the current thread.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(TransactionTask transactionTask) Runs the given task in a regular transaction and returns an operation object without a value.static TransactionOperation<Void> runInTransaction(TransactionTask 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 extends @Nullable Object>
TransactionOperation<T> runInTransaction(ValueSupplier<T> transactionTask) Runs the given supplier in a regular transaction and returns an operation object that wraps the supplier value.static <T extends @Nullable Object>
TransactionOperation<T> runInTransaction(ValueSupplier<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 extends @Nullable Object>
TrunWithoutTransaction(ValueSupplier<T> task) Runs the given supplier outside any transaction and returns the supplied value.static voidsetTransactionFallback(@Nullable SerializableSupplier<@Nullable Transaction> fallback) Sets a supplier that provides a fallback transaction when no explicit transaction is active on the current thread.
-
Constructor Details
-
Transaction
public Transaction()Creates a new transaction.
-
-
Method Details
-
setTransactionFallback
public static void setTransactionFallback(@Nullable SerializableSupplier<@Nullable Transaction> fallback) Sets a supplier that provides a fallback transaction when no explicit transaction is active on the current thread. The supplier is called each time a transaction is needed and no thread-local transaction is set.The supplier should return
nullif no fallback is available for the current context (e.g. no session lock is held).- Parameters:
fallback- the fallback supplier, ornullto remove the fallback
-
getCurrent
Gets the current transaction handler. If no explicit transaction is active on the current thread, a fallback fromsetTransactionFallback(com.vaadin.flow.function.SerializableSupplier<com.vaadin.flow.signals.impl.Transaction>)is used if available. If no fallback is available either, the stateless ROOT transaction is returned.- Returns:
- the current transaction handler, not
null
-
inTransaction
public static boolean inTransaction()Checks whether a transaction is currently active on the current thread. Returnstrueif an explicit transaction is set or if a fallback transaction is available from the signal environment. Returnsfalseif the thread is explicitly running without a transaction.- Returns:
trueif a transaction is active
-
inExplicitTransaction
public static boolean inExplicitTransaction()Checks whether an explicit transaction (started viarunInTransaction(com.vaadin.flow.signals.function.ValueSupplier<T>)) is currently active on the current thread. UnlikeinTransaction(), this method does not consider fallback transactions set viasetTransactionFallback(com.vaadin.flow.function.SerializableSupplier<com.vaadin.flow.signals.impl.Transaction>).This is used by local signal types that are incompatible with transactional semantics to guard against being used in explicit transactions while still allowing use when a session-scoped fallback transaction is active.
- Returns:
trueif an explicit transaction is active
-
runInTransaction
public static <T extends @Nullable Object> TransactionOperation<T> runInTransaction(ValueSupplier<T> transactionTask) 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 extends @Nullable Object> TransactionOperation<T> runInTransaction(ValueSupplier<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(TransactionTask 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
-
createWriteThrough
Creates a new write-through transaction that provides repeatable reads while applying commands immediately to the underlying tree. This is intended for use as a session-scoped fallback transaction.- Returns:
- a new write-through transaction, 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.
-