java transaction api
Download
Skip this Video
Download Presentation
jta-j2ee-pjug-2003-07-22.ppt

Loading in 2 Seconds...

play fullscreen
1 / 43

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


  • 965 Views
  • Uploaded on

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 Presentation

PowerPoint Slideshow about 'jta-j2ee-pjug-2003-07-22.ppt' - guest4006


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

Java Transaction API

Sean C. Sullivan

sean <at> seansullivan <dot> com

July 2003

agenda
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
The J2EE platform

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

definition transaction
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
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
Transaction types
  • Local transactions
  • Distributed transactions
local transaction
Local transaction

Application

Oracle DB

distributed transaction
Distributed transaction

IBM

MQSeries

Transaction

manager

Application

Oracle DB

ERP

system

j2ee transaction specifications
J2EE transaction specifications
  • Java Transaction API (JTA)
  • Java Transaction Service (JTS)
slide10
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
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
J2EE transaction packages

JTA

  • javax.transaction
  • javax.transaction.xa

JTS

  • javax.jts
  • org.omg.CORBA
  • org.omg.CosTransactions
  • org.omg.CosTSPortability
jta in action
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 managers

Resource

manager

Transaction

manager

Application

Resource

manager

Resource

manager

transaction terminology
Transaction terminology
  • Transaction manager
  • Resource manager
  • Resource enlistment
  • XA
  • Two phase commit (2PC)
two phase commit

Prepare

Two phase commit

Transaction

manager

Prepare

Resource

Manager

Prepare

two phase commit cont
Two phase commit (cont.)

Transaction

manager

Prepared

Prepared

Resource

Manager

Prepared

two phase commit cont18

Commit

Two phase commit (cont.)

Transaction

manager

Commit

Resource

Manager

Commit

two phase commit cont19
Two phase commit (cont.)

Transaction

manager

Done

Done

Resource

Manager

Done

transaction demarcation
Transaction demarcation
  • Start a transaction
  • End a transaction
techniques for transaction demarcation
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
Package: javax.transaction
  • javax.transaction.Status
  • javax.transaction.Synchronization
  • javax.transaction.Transaction
  • javax.transaction.TransactionManager
  • javax.transaction.UserTransaction
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
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
Obtaining a UserTransaction in EJB

import javax.transaction.*;

import javax.ejb.*;

// …

private EJBContext ec;

// …

utx = ec.getUserTransaction();

// …

ejb transactions
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
Transactional EJB’s
  • Session beans
    • either CMT or BMT
  • Entity beans
    • always CMT
  • Message driven beans
    • either CMT or BMT
example jta and ejb
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
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
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
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
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
Example: JTA with JMS (cont).

TextMessage msg =

tsess.createTextMessage(“Hello!”);

publisher.publish(msg);

utx.commit();

// …

java data objects jdo
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
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
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
Transactions for web services
  • Protocol specifications:
    • WS-Transaction
    • OASIS Business Transaction Protocol (BTP)
  • Java API
    • JAXTX (JSR-156)
additional topics
Additional topics…
  • Transaction isolation levels
  • Optimistic transactions
  • Nested transactions
  • Extended transaction models (JSR-95)
open source projects
Open source projects

JBossTX

  • http://www.jboss.org/

JOTM

  • http://jotm.objectweb.org/

Tyrex

  • http://tyrex.sourceforge.net/
additional resources
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
Summary
  • If your application accesses multiple data resources, consider using JTA
  • For more details, read the JTA specification
backup slides
Backup slides

These additional slides are backup material.

properties of transactions
Properties of transactions
  • Atomicity
  • Consistentcy
  • Isolated
  • Durable
ad