500 likes | 794 Views
Java Transaction API. Sean C. Sullivan sean <at> seansullivan <dot> com July 2003. Agenda. Java Transaction API (JTA) Using JTA with EJB Using JTA with JDBC Using JTA with JMS Using JTA with JDO Transactions for Web services. The J2EE platform. source: http://java.sun.com/j2ee.
E N D
Java Transaction API Sean C. Sullivan sean <at> seansullivan <dot> com July 2003
Agenda • Java Transaction API (JTA) • Using JTA with EJB • Using JTA with JDBC • Using JTA with JMS • Using JTA with JDO • Transactions for Web services
The J2EE platform source: http://java.sun.com/j2ee
Definition: Transaction “A transaction is a series of operations that appear to execute as one large, atomic operation.” (source: Roman et al, Mastering Enterprise JavaBeans)
Definition: Transaction “A transaction is a complete unit of work. It may comprise many computational tasks, which may include user interface, data retrieval, and communications. A typical transaction modifies shared resources.” (source: The Open Group’s XA specification)
Transaction types • Local transactions • Distributed transactions
Local transaction Application Oracle DB
Distributed transaction IBM MQSeries Transaction manager Application Oracle DB ERP system
J2EE transaction specifications • Java Transaction API (JTA) • Java Transaction Service (JTS)
JTA “JTA is a high level, implementation independent, protocol independent API that allows applications and application servers to access transactions.” source: http://java.sun.com/
JTS “JTS specifies the implementation of a Transaction Manager which supports JTA and implements the Java mapping of the OMG Object Transaction Service (OTS) 1.1 specification at the level below the API. JTS propagates transactions using the Internet Inter-ORB Protocol (IIOP).” source: http://java.sun.com/
J2EE transaction packages JTA • javax.transaction • javax.transaction.xa JTS • javax.jts • org.omg.CORBA • org.omg.CosTransactions • org.omg.CosTSPortability
JTA in action UserTransaction utx = …; try { utx.begin(); transferFunds(your_account, my_swissbank_account, 1000000, US_DOLLARS); travelAgent.purchaseTicket(PDX, MEXICO_CITY); utx.commit() } catch (Exception ex) { utx.rollback(); }
Resource managers Resource manager Transaction manager Application Resource manager Resource manager
Transaction terminology • Transaction manager • Resource manager • Resource enlistment • XA • Two phase commit (2PC)
Prepare Two phase commit Transaction manager Prepare Resource Manager Prepare
Two phase commit (cont.) Transaction manager Prepared Prepared Resource Manager Prepared
Commit Two phase commit (cont.) Transaction manager Commit Resource Manager Commit
Two phase commit (cont.) Transaction manager Done Done Resource Manager Done
Transaction demarcation • Start a transaction • End a transaction
Techniques for transaction demarcation • Declarative • programmer declares transaction attributes • runtime environment uses attributes to manage transactions • Programmatic • programmer is responsible for coding transaction logic • application controls a transaction via an API
Package: javax.transaction • javax.transaction.Status • javax.transaction.Synchronization • javax.transaction.Transaction • javax.transaction.TransactionManager • javax.transaction.UserTransaction
javax.transaction.UserTransaction Methods: • public void begin() • public void commit() • public void rollback() • public void setRollbackOnly() • public void setTransactionTimeout(int) • public int getStatus()
Obtaining a UserTransaction via JNDI import javax.transaction.*; import javax.naming.*; // … InitialContext ctx = new InitialContext(); obj = ctx.lookup( “java:/comp/UserTransaction”); UserTransaction tx = (UserTransaction) obj; // …
Obtaining a UserTransaction in EJB import javax.transaction.*; import javax.ejb.*; // … private EJBContext ec; // … utx = ec.getUserTransaction(); // …
EJB transactions • Declarative • Container-Managed Transactions (CMT) • Transaction attributes declared in EJB deployment descriptor (ejb-jar.xml) • Programmatic • Bean-Managed Transactions (BMT)
Transactional EJB’s • Session beans • either CMT or BMT • Entity beans • always CMT • Message driven beans • either CMT or BMT
Example: JTA and EJB public void deposit(double amount) { UserTransaction utx = ctx.getUserTransaction(); try { utx.begin(); updateAccount(amount); utx.commit(); } catch (Exception ex) { utx.rollback(); } }
JTA and JDBC If the JDBC driver implements the XADataSource interface, the database can participate as a resource manager in a JTA transaction
Using JTA and JDBC 1) Configure an XA DataSource 2) Lookup DataSource via JNDI 3) Lookup UserTransaction via JNDI 4) Invoke utx.begin() 5) Invoke DataSource.getConnection() 6) Execute SQL statements 7) Invoke utx.commit() 8) Invoke java.sql.Connection.close()
JTA and JMS If the JMS provider supports the XAResource interface, JMS can participate as a resource manager in a JTA transaction
Example: JTA with JMS import javax.jms.*; import javax.transaction.*; // TopicSession tsess = …; Topic top = …; UserTransaction utx = lookupUsingJNDI(); utx.begin(); TopicPublisher publisher = tsess.createPublisher(top); // …
Example: JTA with JMS (cont). TextMessage msg = tsess.createTextMessage(“Hello!”); publisher.publish(msg); utx.commit(); // …
Java Data Objects (JDO) • javax.jdo.PersistenceManagerFactory • javax.jdo.PersistenceManager • method: currentTransaction() • javax.jdo.Transaction • method: begin() • method: commit() • method: rollback()
Example: JDO local transaction import javax.jdo.*; PersistenceManagerFactory pmf = …; PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); // ... shirt.setColor(WHITE); tx.commit(); } catch (Exception ex) { tx.rollback(); }
Example: JDO and JTA import javax.jdo.*; import javax.transaction.*; UserTransaction utx = …; try { utx.begin(); PersistenceManager pm = pmf.getPersistenceManager(); // … shirt.setColor(BLUE); utx.commit(); } catch (Exception ex) { utx.rollback(); }
Transactions for web services • Protocol specifications: • WS-Transaction • OASIS Business Transaction Protocol (BTP) • Java API • JAXTX (JSR-156)
Additional topics… • Transaction isolation levels • Optimistic transactions • Nested transactions • Extended transaction models (JSR-95)
Open source projects JBossTX • http://www.jboss.org/ JOTM • http://jotm.objectweb.org/ Tyrex • http://tyrex.sourceforge.net/
Additional resources • http://java.sun.com/products/jta/ • http://java.sun.com/products/jts/ • http://java.sun.com/products/jdbc/ • http://java.sun.com/products/jdo/ • http://java.sun.com/products/jms/ • http://java.sun.com/products/ejb/ • http://java.sun.com/j2ee/
Summary • If your application accesses multiple data resources, consider using JTA • For more details, read the JTA specification
Backup slides These additional slides are backup material.
Properties of transactions • Atomicity • Consistentcy • Isolated • Durable