Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Transaction API Sean C. Sullivan

Similar presentations


Presentation on theme: "Java Transaction API Sean C. Sullivan"— 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:

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
Distributed transaction a.k.a. “Global transaction” The JTA spec refers to “Local transactions” and “Global transactions”

7 Local transaction Application Oracle DB
“The coordination of [a local transaction] involves no external transaction managers” (Dibyendu Baksi,

8 Distributed transaction
IBM MQSeries Transaction manager Application Oracle DB Example resource managers: Oracle DB, IBM DB2, SAP, IBM MQSeries, SonicMQ JTA 1.0.1b specification: “A transaction manager provides the services and management functions required to support transaction demarcation, transactional resource management, synchronization, and transaction context propagation.” “A resource manager (through a resource adapter) provides the application access to resources. The resource manager participates in distributed transactions by implementing a transaction resource interface used by the transaction manager to communicate transaction association, transaction completion and recovery work. An example of such a resource manager is a relational database server.” 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:

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:

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, , US_DOLLARS); travelAgent.purchaseTicket(PDX, MEXICO_CITY); utx.commit() } catch (Exception ex) utx.rollback();

14 Resource managers Resource manager Transaction manager Application
Example resource managers: Oracle DB, IBM DB2, SAP, IBM MQSeries, SonicMQ JTA 1.0.1b specification: “A transaction manager provides the services and management functions required to support transaction demarcation, transactional resource management, synchronization, and transaction context propagation.” “A resource manager (through a resource adapter) provides the application access to resources. The resource manager participates in distributed transactions by implementing a transaction resource interface used by the transaction manager to communicate transaction association, transaction completion and recovery work. An example of such a resource manager is a relational database server.” Resource manager

15 Transaction terminology
Transaction manager Resource manager Resource enlistment XA Two phase commit (2PC) JTA 1.0.1b specification: “A transaction manager provides the services and management functions required to support transaction demarcation, transactional resource management, synchronization, and transaction context propagation.” “A resource manager (through a resource adapter) provides the application access to resources. The resource manager participates in distributed transactions by implementing a transaction resource interface used by the transaction manager to communicate transaction association, transaction completion and recovery work. An example of such a resource manager is a relational database server.” Open Group XA specification “Resource managers (RMs, such as databases or file access systems) provide access to shared resources.” “A separate component called a transaction manager (TM) assigns identifiers to transactions, monitors their progress, and takes responsibility for transaction completion and for failure recovery.” “XA interface: the bidirectional interface between a transaction manager and a resource manager.” by Dibyendu Baksi “[…] resource manager […] enables the access and manipulation of data or resources” “Resource managers inform the transaction manager of their participation in a transaction By means of a process called resource enlistment”

16 Two phase commit Prepare Transaction manager Prepare Resource Manager

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

18 Two phase commit (cont.)
Transaction manager Commit 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 Declarative: application declares transactional attributes. The runtime environment uses these attributes to manage transactions. (EJB, Microsoft Transaction Server) Programmatic: the programmer is responsible for coding transaction logic in the application

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 Programmatic
Container-Managed Transactions (CMT) Transaction attributes declared in EJB deployment descriptor (ejb-jar.xml) Programmatic Bean-Managed Transactions (BMT) For more information, read Ed Roman’s book “Mastering Enterprise JavaBeans” (2nd edition)

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

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 JOTM Tyrex http://www.jboss.org/
Tyrex

40 Additional resources http://java.sun.com/products/jta/

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


Download ppt "Java Transaction API Sean C. Sullivan"

Similar presentations


Ads by Google