Class Transactions
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classWhat one call tobegin(int, boolean, int)did, which the matchingcommit(Transactions.Transaction)orafterThrow(Transactions.Transaction, boolean)undoes. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final int -
Method Summary
Modifier and TypeMethodDescriptionstatic voidafterThrow(Transactions.Transaction tx, boolean rollback) Ends a call that threw.static Transactions.Transactionbegin(int propagation, boolean readOnly, int timeoutSeconds) Starts what a@Transactionalmethod asked for.static voidEnds a call that returned normally.static booleanisActive()Whether the calling thread is inside a transaction.static booleanisActiveOn(DataSource pool) Whether the calling thread's transaction is onpool, or could be.static booleanisJoined(DataSource pool, Database db) Whetherdbis the connection of the calling thread's transaction onpool.static booleanWhether the calling thread's transaction has been marked rollback-only.static Databasejoined(DataSource pool) The transaction's connection forpool, borrowing it and sending BEGIN the first time; null when the thread is not in a transaction or its transaction is on another database.static voidCalled by the session adapter when a session that joined this thread's transaction rolls back: the rows are not the session's to keep, so the transaction they are in cannot commit either.static voidmarkRollbackOnly(DataSource pool) static Sessionsession(EntityManager entities) The managed session of the calling thread's transaction, opened on the transaction's connection the first time it is asked for and flushed and closed when the transaction ends.static voidMarks 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.
-
Field Details
-
REQUIRED
public static final int REQUIRED- See Also:
-
SUPPORTS
public static final int SUPPORTS- See Also:
-
MANDATORY
public static final int MANDATORY- See Also:
-
REQUIRES_NEW
public static final int REQUIRES_NEW- See Also:
-
NOT_SUPPORTED
public static final int NOT_SUPPORTED- See Also:
-
NEVER
public static final int NEVER- See Also:
-
NESTED
public static final int NESTED- See Also:
-
-
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. -
begin
Starts what a
@Transactionalmethod asked for. Called by woven code.Parameters
-
propagation: one of the constants above -
readOnly: the annotation's readOnly -
timeoutSeconds: the annotation's timeout, zero or less for none
-
-
commit
Ends a call that returned normally. Commits when the call began the transaction, and does nothing more than restore state when it joined one. -
afterThrow
Ends a call that threw. Rolls back -- the call's own transaction, its savepoint, or, when it joined one, by marking the shared transaction rollback-only -- when
rollbackis true, and otherwise treats the exception as a normal end, which is Spring's rule for a checked one.Never throws: the woven code rethrows the method's own exception after this returns, and a failure here would replace the cause with a symptom. A rollback that fails is reported on standard error and its connection closed, so the pool cannot hand out a session that is still inside a transaction.
-
joined
The transaction's connection forpool, borrowing it and sending BEGIN the first time; null when the thread is not in a transaction or its transaction is on another database. Called byDataSource.borrow().- Throws:
IOException
-
isJoined
Whetherdbis the connection of the calling thread's transaction onpool. -
isActiveOn
Whether the calling thread's transaction is onpool, or could be. -
session
The managed session of the calling thread's transaction, opened on the transaction's connection the first time it is asked for and flushed and closed when the transaction ends.
This is what an injected
Sessiondelegates to. Outside a transaction there is no session to give, and the refusal says where one comes from. -
markRollbackOnly
public static void markRollbackOnly()Called by the session adapter when a session that joined this thread's transaction rolls back: the rows are not the session's to keep, so the transaction they are in cannot commit either. Marks the calling thread's transaction, whatever pool it is on, as unable to commit; nothing when there is none. -
markRollbackOnly
-