JTA | Developer Conference JBoss Transactions Ivo Studenský JBoss QA Engineer, Jiří Pechanec JBoss QE Supervisor, Red Hat Sep 10th, 2009
JTA | Developer Conference Agenda ● JBoss Transactions Overview ● JTA Overview ● Application Interfaces ● Container Interfaces ● Demonstration ● User Transaction ● Transaction Manager ● Sample Resource Implementation ● Synchronization
JTA | Developer Conference JBoss Transactions ● JBoss Transactions is a rebadge of the Arjuna Transaction Service which supports high performance, high reliability transaction processing, compliant with JTA, JTS and Web Services standards. ● ● Standards compliance ● CORBA Object Transaction Service (OTS) ● Java Enterprise (JEE) transactions ● Java Transaction API (JTA) ● Java Transaction Service (JTS) ● Web services transactions ● Web Services Coordination (WS-Coordination) ● Web Services Atomic Transaction (WS-AtomicTransaction) ● Web Services Business Activity Framework (WS-BusinessActivity)
JTA | Developer Conference Java Enterprise transactions ● Java Transaction API (JTA) ● JTA specifies standard Java interfaces between a transaction manager and the parties involved in a distributed transaction system: the resource manager, the application server, and the transactional applications. ● JTA is a specification developed under the Java Community Process as JSR 907. ● Java Transaction Service (JTS) ● JTS specifies the implementation of a transaction manager that supports JTA specification at the high level and implements the Java mapping of the OMG Object Transaction Service (OTS) specification at the low level. JTS uses the standard CORBA ORB/TS interfaces and Internet Inter- ORB Protocol (IIOP) for transaction context propagation between JTS transaction managers.
JTA | Developer Conference JTA Composition ● High-level application interface for transaction boundaries demarcation ● High-level transaction manager interface used by container to control transactions ● Java mapping to X/Open XA protocol to participate in transaction controlled by external transaction manager
JTA | Developer Conference Transactional services in JEE
JTA | Developer Conference Application interface ● Interface javax.transaction.UserTransaction ● Used by JEE applications to programmatically demarcate transactions ● Obtained via JNDI (java:comp/UserTransaction) or using resource annotation ● Transactions are associated with running thread ● Provides basic operations ● begin ● commit ● rollback ● setRollbackOnly ● getStatus ● Application only manages transaction, the resources that participates in transaction are managed by container
JTA | Developer Conference UserTransaction – basic operations ● begin() ● create a new transaction and associate it with the current thread ● commit() ● complete the transaction associated with the current thread ● rollback() ● roll back the transaction associated with the current thread ● setRollbackOnly() ● modify the transaction associated with the current thread such that the only possible outcome of the transaction is to roll back the transaction. ● getStatus() ● obtain the status of the transaction associated with the current thread
JTA | Developer Conference Container Transaction Management
JTA | Developer Conference Transaction Manager ● Interface javax.transaction.TransactionManager ● Used by container ● Can be obtained by application – container specific ● JBoss – via JNDI (java:/TransactionManager) ● Supports not only starting and ending transactions but also suspension – used for session bean transactional attributes ● begin ● commit ● rollback ● setRollbackOnly ● getStatus ● getTransaction ● suspend ● resume
JTA | Developer Conference TransactionManager – basic operations ● begin() - creates a new transaction and associate it with the current thread ● commit() - completes the transaction associated with the current thread ● rollback() - rolls back the transaction associated with the current thread ● setRollbackOnly() - modifies the outcome of the transaction to roll back ● int getStatus() - obtains the status of the transaction associated with the current thread ● Transaction getTransaction() - gets the transaction object that represents the transaction context of the calling thread ● Transaction suspend() - suspends the transaction currently associated with the calling thread and return a Transaction object that represents the transaction context being suspended ● resume(Transaction t) - resumes the transaction context association of the calling thread with the transaction represented by the supplied Transaction object.
JTA | Developer Conference Transaction ● Interface javax.transaction.Transaction ● Represents transaction started by transaction manager ● Controls transaction outcome ● commit ● rollback ● setRollbackOnly ● Enlists/delists resources to transactions ● enlistResource ● delistResource ● Registers synchronizations with transactions ● registerSynchronization
JTA | Developer Conference Transaction – basic actions ● commit() ● rollback() ● setRollbackOnly() ● enlistResource(XAResource xaRes) ● enlists the resource specified with the transaction associated with the target Transaction object ● delistResource(XAResource xaRes, int flag) ● disassociates the resource specified from the transaction associated with the target Transaction object ● registerSynchronization(Synchronization sync) ● registers a synchronization object for the transaction currently associated with the target object
JTA | Developer Conference Synchronization ● Interface javax.transaction.Synchronization ● Callback interface to inform interested components about transaction completion ● beforeCompletion() - called by the transaction manager prior to the start of the two-phase transaction commit process ● afterCompletion(int status) - called by the transaction manager after the transaction is committed or rolled back ● Typically used by cache to update/invalidate its contents ● Best-effort only, not guaranteed to be called in case of crash
JTA | Developer Conference XA Resource ● Interface javax.transaction.xa.XAResource ● Represents any object that supports one or two phase protocol to participate in transaction and can ensure ACID properties ● Database connections ● JMS Connections ● Methods are called exclusively by Transaction Manager ● start ● end ● prepare ● commit ● rollback ● recover ● forget
JTA | Developer Conference XAResource – basic actions ● start(Xid xid, int flags) - starts work on behalf of a transaction branch specified in xid ● end(Xid xid, int flags) - ends the work performed on behalf of a transaction branch ● prepare(Xid xid) - asks the resource manager to prepare for a transaction commit of the transaction specified in xid ● commit(Xid xid, boolean onePhase) - commits the global transaction specified by xid ● rollback(Xid xid) - informs the resource manager to roll back work done on behalf of a transaction branch ● Xid[] recover(int flag) - obtains a list of prepared transaction branches from a resource manager ● forget(Xid xid) - tells the resource manager to forget about a heuristically completed transaction branch
JTA | Developer Conference XID ● Interface javax.transaction.xa.Xid ● Unique identifier of transaction ● Industry standard ● fully portable across different Transaction Managers, allows creation of delegated transactions in case of hierarchical Transaction Managers ● Consists of three components ● Format identifier – must be unique across the transaction systems ● Global transaction identifier ● Branch qualifier
JTA | Developer Conference Logical transaction flow
JTA | Developer Conference Example 1 – User Transaction ● Start-up user transaction ● Show how the database is enlisted to transaction behind the scenes ● Show how the database connection is rolled back/committed when JTA transaction is ended
JTA | Developer Conference Example 2 – Transaction Manager ● Start-up transaction ● Update database in transaction and suspend it ● Update database in another transaction ● Resume and complete suspended transaction
JTA | Developer Conference Example 3 – Sample Resource ● Create XA resource representing File ● Simulate two phase commit ● Prepare phase – temporary file ● Commit phase – move temporary file to final one ● Show recovery after TM crash
JTA | Developer Conference Example 4 - Synchronization ● Print the message when Synchronization is called ● Show when the events are fired
JTA | Developer Conference Questions ?
JTA | Developer Conference Transaction Fundamentals ● ACID ● Atomicity – the transaction completes successfully (commits) or if it fails (aborts) all of its effects are undone (rolled back) ● Consistency – transactions produce consistent results and preserve application-specific invariants, i.e. preserves the internal consistency of the data it acts on ● Isolation – intermediate states produced while a transaction is executing are not visible to others (serializability) ● Durability – the effects of a committed transaction are never lost (except by a catastrophic failure) ● Transaction can be terminated in two ways: committed or aborted (rolled back).