Class JpaTransactions


  • public final class JpaTransactions
    extends Object
    Static access to the transaction the calling thread runs in, for code that can have nothing injected. A CDI bean injects an EntityManager or the JpaTransactionRunner instead of using this class.

    These methods only report on and influence a transaction that is already running, they never open one. Work runs in a transaction through @JpaTransactional or the JpaTransactionRunner.

    Available where the persistence implementation is, which is the master server.

    Since:
    8.5.6
    Author:
    Norman Lorenz
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void afterCommit​(Runnable action)
      Runs the given action once the transaction the calling thread runs in has been committed, for work that must not take effect before the unit of work is durable, such as publishing an event.
      static boolean isActive()
      Whether the calling thread runs in a transaction, that is whether it is inside a unit of work.
      static void markRollbackOnly()
      Prevents the transaction the calling thread runs in from being committed, for work that found a reason not to commit but does not report it as an exception.
    • Method Detail

      • afterCommit

        public static void afterCommit​(Runnable action)
        Runs the given action once the transaction the calling thread runs in has been committed, for work that must not take effect before the unit of work is durable, such as publishing an event. A rolled back transaction discards its actions without running them.

        The action cannot use the entity manager of the transaction, it is closed by then. See JpaTransactionRunner.afterCommit(Runnable) for the full contract.

        Parameters:
        action - the action to run after the commit. Must not be null.
        Throws:
        IllegalArgumentException - if the action is null.
        IllegalStateException - if the calling thread does not run in a transaction, or the persistence implementation is not available.
        Since:
        8.5.6
      • isActive

        public static boolean isActive()
        Whether the calling thread runs in a transaction, that is whether it is inside a unit of work.
        Returns:
        true if the calling thread runs in a transaction.
        Throws:
        IllegalStateException - if the persistence implementation is not available.
        Since:
        8.5.6
      • markRollbackOnly

        public static void markRollbackOnly()
        Prevents the transaction the calling thread runs in from being committed, for work that found a reason not to commit but does not report it as an exception. The unit of work still returns normally.
        Throws:
        IllegalStateException - if the calling thread does not run in a transaction, or the persistence implementation is not available.
        Since:
        8.5.6