2.2. TransactionManager
The
TransactionManager
interface allows the application server to control transaction boundaries on behalf of the application being managed.
Note
You can obtain a
TransactionManager
from JNDI.
InitialContext ic = new InitialContext(); TransactionManager utm = ic.lookup("java:/TransactionManager")
The Transaction Manager maintains the transaction context association with threads as part of its internal data structure. A thread’s transaction context is either
null
or it refers to a specific global transaction. Multiple threads can be associated with the same global transaction. Nested transactions are not supported.
Each transaction context is encapsulated within a Transaction object, which can be used to perform operations which are specific to the target transaction, regardless of the calling thread’s transaction context.
The
begin
method of TransactionManager
begins a new top-level transaction, and associates the transaction context with the calling thread. If the calling thread is already associated with a transaction then the begin
method throws the NotSupportedException
.
The
getTransaction
method returns the Transaction object that represents the transaction context currently associated with the calling thread. This object can be used to perform various operations on the target transaction. These operations are described elsewhere.
The
commit
method completes the transaction currently associated with the calling thread. After it returns, the calling thread is not associated with any transaction. If commit
is called when the thread is not associated with any transaction context, an exception is thrown. In some implementations, only the transaction originator can use the commit
operation. If the calling thread is not permitted to commit the transaction, an exception is thrown. JBossJTA does not impose any restrictions on the ability of threads to terminate transactions.
The
rollback
method is used to roll back the transaction associated with the current thread. After the rollback
method completes, the thread is not associated with any transaction.
Note
In a multi-threaded environment, multiple threads may be active within the same transaction. If checked transaction semantics have been disabled, or the transaction times out, then a transaction can be terminated by a thread other than its creator. If this happens, the creator must be notified. JBoss Transaction Service does this notification during commit or rollback by throwing the
IllegalStateException
exception.