Class Transactions

java.lang.Object
com.codename1.backend.Transactions

public final class Transactions extends Object

The transaction a @Transactional method runs in, bound to the calling thread.

The build rewrites every @Transactional method into a call to begin(int, boolean, int), the original body, then commit(Transactions.Transaction) -- or afterThrow(Transactions.Transaction, boolean) with the rollback decision it worked out from the annotation. The propagation, the read-only flag and the timeout arrive as constants, so this class interprets nothing: it holds the one piece of state that has to exist at run time, which is which connection the thread's transaction is on.

Joining

Nothing has to be handed the transaction. DataSource.borrow() asks this class first, and a thread inside a transaction gets the transaction's connection back instead of a pooled one -- so the pool's own methods, an EntityManager's daos and a managed session all join it. The connection is borrowed, and BEGIN sent, only when the transaction first needs it: a @Transactional method that returns before touching the database costs a thread-local write and nothing else.

A thread that has never begun a transaction does not even read the thread-local: used is set by the first begin(int, boolean, int) of the process, on the thread that is then inside it, so every other thread reading a stale false is correct -- none of them is in a transaction.

Propagation

Spring's seven, one for one; the constants below are the ordinals of com.codename1.backend.annotations.Propagation. A method that joins a transaction and fails with a rollback-worthy exception marks the whole transaction rollback-only, and the method that began it then rolls back and throws TransactionException.UnexpectedRollback rather than reporting a commit that did not happen.

  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Whether the calling thread is inside a transaction.
    static boolean
    Whether the calling thread's transaction has been marked rollback-only.
    static void
    Marks the calling thread's transaction so it rolls back however its method ends -- the programmatic form of throwing, for a method that wants to return a value and still undo its work.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isActive

      public static boolean isActive()
      Whether the calling thread is inside a transaction.
    • isRollbackOnly

      public static boolean isRollbackOnly()
      Whether the calling thread's transaction has been marked rollback-only.
    • setRollbackOnly

      public static void setRollbackOnly()
      Marks the calling thread's transaction so it rolls back however its method ends -- the programmatic form of throwing, for a method that wants to return a value and still undo its work.