com.vaadin.signals.impl.

Class Transaction

java.lang.Object
com.vaadin.signals.impl.Transaction

Direct Known Subclasses:

StagedTransaction

public abstract class Transaction extends Object

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.

  • Constructor Details

    • Transaction

      public Transaction()
  • Method Details

    • getCurrent

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

      public 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. 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> 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 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(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, not null

      transactionType - the type of the transaction, not null

      Returns:

      the operation object, not null

    • runInTransaction

      public static TransactionOperation<Void> runInTransaction(Runnable 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> T runWithoutTransaction(Supplier<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(Runnable 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

    • 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, 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, 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. 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.

    • 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.