1 / 43

Java Transaction API

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.

leop
Download Presentation

Java Transaction API

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Transaction API Sean C. Sullivan sean <at> seansullivan <dot> com July 2003

  2. 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

  3. The J2EE platform source: http://java.sun.com/j2ee

  4. 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)

  5. 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)

  6. Transaction types • Local transactions • Distributed transactions

  7. Local transaction Application Oracle DB

  8. Distributed transaction IBM MQSeries Transaction manager Application Oracle DB ERP system

  9. J2EE transaction specifications • Java Transaction API (JTA) • Java Transaction Service (JTS)

  10. 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/

  11. 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/

  12. J2EE transaction packages JTA • javax.transaction • javax.transaction.xa JTS • javax.jts • org.omg.CORBA • org.omg.CosTransactions • org.omg.CosTSPortability

  13. 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(); }

  14. Resource managers Resource manager Transaction manager Application Resource manager Resource manager

  15. Transaction terminology • Transaction manager • Resource manager • Resource enlistment • XA • Two phase commit (2PC)

  16. Prepare Two phase commit Transaction manager Prepare Resource Manager Prepare

  17. Two phase commit (cont.) Transaction manager Prepared Prepared Resource Manager Prepared

  18. Commit Two phase commit (cont.) Transaction manager Commit Resource Manager Commit

  19. Two phase commit (cont.) Transaction manager Done Done Resource Manager Done

  20. Transaction demarcation • Start a transaction • End a transaction

  21. 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

  22. Package: javax.transaction • javax.transaction.Status • javax.transaction.Synchronization • javax.transaction.Transaction • javax.transaction.TransactionManager • javax.transaction.UserTransaction

  23. javax.transaction.UserTransaction Methods: • public void begin() • public void commit() • public void rollback() • public void setRollbackOnly() • public void setTransactionTimeout(int) • public int getStatus()

  24. 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; // …

  25. Obtaining a UserTransaction in EJB import javax.transaction.*; import javax.ejb.*; // … private EJBContext ec; // … utx = ec.getUserTransaction(); // …

  26. EJB transactions • Declarative • Container-Managed Transactions (CMT) • Transaction attributes declared in EJB deployment descriptor (ejb-jar.xml) • Programmatic • Bean-Managed Transactions (BMT)

  27. Transactional EJB’s • Session beans • either CMT or BMT • Entity beans • always CMT • Message driven beans • either CMT or BMT

  28. 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(); } }

  29. JTA and JDBC If the JDBC driver implements the XADataSource interface, the database can participate as a resource manager in a JTA transaction

  30. 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()

  31. JTA and JMS If the JMS provider supports the XAResource interface, JMS can participate as a resource manager in a JTA transaction

  32. Example: JTA with JMS import javax.jms.*; import javax.transaction.*; // TopicSession tsess = …; Topic top = …; UserTransaction utx = lookupUsingJNDI(); utx.begin(); TopicPublisher publisher = tsess.createPublisher(top); // …

  33. Example: JTA with JMS (cont). TextMessage msg = tsess.createTextMessage(“Hello!”); publisher.publish(msg); utx.commit(); // …

  34. Java Data Objects (JDO) • javax.jdo.PersistenceManagerFactory • javax.jdo.PersistenceManager • method: currentTransaction() • javax.jdo.Transaction • method: begin() • method: commit() • method: rollback()

  35. 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(); }

  36. 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(); }

  37. Transactions for web services • Protocol specifications: • WS-Transaction • OASIS Business Transaction Protocol (BTP) • Java API • JAXTX (JSR-156)

  38. Additional topics… • Transaction isolation levels • Optimistic transactions • Nested transactions • Extended transaction models (JSR-95)

  39. Open source projects JBossTX • http://www.jboss.org/ JOTM • http://jotm.objectweb.org/ Tyrex • http://tyrex.sourceforge.net/

  40. 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/

  41. Summary • If your application accesses multiple data resources, consider using JTA • For more details, read the JTA specification

  42. Backup slides These additional slides are backup material.

  43. Properties of transactions • Atomicity • Consistentcy • Isolated • Durable

More Related