Java transaction api
Download
1 / 43

Jta-j2ee-pjug-2003-07-22.ppt - PowerPoint PPT Presentation

jta-j2ee-pjug-2003-07-22.ppt

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha

Download Presentationdownload

jta-j2ee-pjug-2003-07-22.ppt

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Java transaction api l.jpg

Java Transaction API

Sean C. Sullivan

sean <at> seansullivan <dot> com

July 2003


Agenda l.jpg

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 l.jpg

The J2EE platform

source: http://java.sun.com/j2ee


Definition transaction l.jpg

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 transaction5 l.jpg

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 l.jpg

Transaction types

  • Local transactions

  • Distributed transactions


Local transaction l.jpg

Local transaction

Application

Oracle DB


Distributed transaction l.jpg

Distributed transaction

IBM

MQSeries

Transaction

manager

Application

Oracle DB

ERP

system


J2ee transaction specifications l.jpg

J2EE transaction specifications

  • Java Transaction API (JTA)

  • Java Transaction Service (JTS)


Slide10 l.jpg

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/


Slide11 l.jpg

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 l.jpg

J2EE transaction packages

JTA

  • javax.transaction

  • javax.transaction.xa

    JTS

  • javax.jts

  • org.omg.CORBA

  • org.omg.CosTransactions

  • org.omg.CosTSPortability


Jta in action l.jpg

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 l.jpg

Resource managers

Resource

manager

Transaction

manager

Application

Resource

manager

Resource

manager


Transaction terminology l.jpg

Transaction terminology

  • Transaction manager

  • Resource manager

  • Resource enlistment

  • XA

  • Two phase commit (2PC)


Two phase commit l.jpg

Prepare

Two phase commit

Transaction

manager

Prepare

Resource

Manager

Prepare


Two phase commit cont l.jpg

Two phase commit (cont.)

Transaction

manager

Prepared

Prepared

Resource

Manager

Prepared


Two phase commit cont18 l.jpg

Commit

Two phase commit (cont.)

Transaction

manager

Commit

Resource

Manager

Commit


Two phase commit cont19 l.jpg

Two phase commit (cont.)

Transaction

manager

Done

Done

Resource

Manager

Done


Transaction demarcation l.jpg

Transaction demarcation

  • Start a transaction

  • End a transaction


Techniques for transaction demarcation l.jpg

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 l.jpg

Package: javax.transaction

  • javax.transaction.Status

  • javax.transaction.Synchronization

  • javax.transaction.Transaction

  • javax.transaction.TransactionManager

  • javax.transaction.UserTransaction


Javax transaction usertransaction l.jpg

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 l.jpg

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 l.jpg

Obtaining a UserTransaction in EJB

import javax.transaction.*;

import javax.ejb.*;

// …

private EJBContext ec;

// …

utx = ec.getUserTransaction();

// …


Ejb transactions l.jpg

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 l.jpg

Transactional EJB’s

  • Session beans

    • either CMT or BMT

  • Entity beans

    • always CMT

  • Message driven beans

    • either CMT or BMT


Example jta and ejb l.jpg

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 l.jpg

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 l.jpg

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 l.jpg

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 l.jpg

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 l.jpg

Example: JTA with JMS (cont).

TextMessage msg =

tsess.createTextMessage(“Hello!”);

publisher.publish(msg);

utx.commit();

// …


Java data objects jdo l.jpg

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 l.jpg

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 l.jpg

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 l.jpg

Transactions for web services

  • Protocol specifications:

    • WS-Transaction

    • OASIS Business Transaction Protocol (BTP)

  • Java API

    • JAXTX (JSR-156)


Additional topics l.jpg

Additional topics…

  • Transaction isolation levels

  • Optimistic transactions

  • Nested transactions

  • Extended transaction models (JSR-95)


Open source projects l.jpg

Open source projects

JBossTX

  • http://www.jboss.org/

    JOTM

  • http://jotm.objectweb.org/

    Tyrex

  • http://tyrex.sourceforge.net/


Additional resources l.jpg

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 l.jpg

Summary

  • If your application accesses multiple data resources, consider using JTA

  • For more details, read the JTA specification


Backup slides l.jpg

Backup slides

These additional slides are backup material.


Properties of transactions l.jpg

Properties of transactions

  • Atomicity

  • Consistentcy

  • Isolated

  • Durable


ad
  • Login