Class Transaction

java.lang.Object
com.vaadin.flow.signals.impl.Transaction
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
StagedTransaction

public abstract class Transaction extends Object implements Serializable
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:
  • 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 null if no fallback is available for the current context (e.g. no session lock is held).

      Parameters:
      fallback - the fallback supplier, or null to remove the fallback
    • getCurrent

      public static Transaction getCurrent()
      Gets the current transaction handler. If no explicit transaction is active on the current thread, a fallback from setTransactionFallback(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. Returns true if an explicit transaction is set or if a fallback transaction is available from the signal environment. Returns false if the thread is explicitly running without a transaction.
      Returns:
      true if a transaction is active
    • inExplicitTransaction

      public static boolean inExplicitTransaction()
      Checks whether an explicit transaction (started via runInTransaction(com.vaadin.flow.signals.function.ValueSupplier<T>)) is currently active on the current thread. Unlike inTransaction(), this method does not consider fallback transactions set via setTransactionFallback(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:
      true if 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 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 type
      Parameters:
      transactionTask - the supplier to run in a transaction, not null
      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 from getCurrent().
      Type Parameters:
      T - the supplier type
      Parameters:
      transactionTask - the supplier to run in a transaction, not null
      transactionType - the type of the transaction, not null
      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 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, not null
      transactionType - the type of the transaction, not null
      Returns:
      the operation object, not null
    • runInTransaction

      public static TransactionOperation<Void> runInTransaction(TransactionTask transactionTask)
      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, not null
      Returns:
      the operation object, not null
    • runWithoutTransaction

      public static <T extends @Nullable Object> T runWithoutTransaction(ValueSupplier<T> task)
      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, not null
      Returns:
      the value returned from the supplier
    • runWithoutTransaction

      public static void runWithoutTransaction(TransactionTask task)
      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, not null
    • createWriteThrough

      public static Transaction 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
    • include

      public abstract void include(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. 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, not null
      command - the command to include, not null
      resultHandler - the handler of the command result, or null to ignore the result
      applyToTree - true to apply the command to the underlying tree, false to only update the transaction's repeatable-read revision
    • include

      public void include(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. 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, not null
      command - the command to apply, not null
      resultHandler - the handler of the command result, or null to ignore the result
    • read

      public abstract TreeRevision read(SignalTree tree)
      Gets a revision for reading from the given tree in the context of this transaction.
      Parameters:
      tree - the tree to read from, not null
      Returns:
      a tree revision to read from, not null
    • commit

      protected abstract void commit(Consumer<SignalOperation.ResultOrError<Void>> resultHandler)
      Commits any staged commands in this transaction.
      Parameters:
      resultHandler - a consumer to update the result value in the corresponding transaction operation, not null
    • rollback

      protected abstract void rollback()
      Rolls back any staged commands in this transaction and notifies the result handlers for those commands.