Sie sind auf Seite 1von 10

Understanding JTAthe Java Transaction API

JTA allows applications to perform distributed transactionsto access and update data on two or more
networked computer resources. JTA support in a JDBC driver yields greatly increased data access
power. Heres !ow.
!e purpose of t!is article is to provide a !ig!"level overview of t!e Java Transaction
A#$ %JTA&' and !ow it is related to distributed transactions. A transaction defines a
logical unit of work t!at eit!er completely succeeds or produces no result at all. A
distributed transaction is simply a transaction t!at accesses and updates data on two or
more networked resources' and t!erefore must be coordinated among t!ose resources.
$n t!is paper' we are concerned primarily wit! transactions t!at involve relational
database systems.
T!e components involved in t!e distributed transaction processing %DT#& model t!at
are relevant to our discussion are(
T!e application
T!e application server
T!e transaction manager
T!e resource adapter
T!e resource manager
$n t!e following sections' we describe t!ese components and t!eir relations!ip to JTA
and database access.
Accessing Databases
$t is best to t!ink of t!e components involved in distributed transactions as independent
processes' rat!er t!an t!inking of t!em in terms of location on a particular computer.
)everal of t!e components may reside on one mac!ine' or t!ey may be spread among
several mac!ines. T!e diagrams in t!e following e*amples may s!ow a component on
a particular computer' but t!e relations!ip among t!e processes is t!e primary
consideration.
The Simplest Case: Application to Database Local Transactions
T!e simplest form of relational database access involves only t!e application' a
resource manager' and a resource adapter. T!e application is simply t!e end"user
access point to send re+uests to' and obtain data from' a database.
T!e resource manager in our discussion is a relational database management system
%,DB-)&' suc! as .racle or )/0 )erver. All of t!e actual database management is
!andled by t!is component.
T!e resource adapter is t!e component t!at is t!e communications c!annel' or re+uest
translator' between t!e 1outside world'1 in t!is case t!e application' and t!e resource
manager. 2or our discussion' t!is is a JDBC driver.
T!e following description is of a resource manager local transaction' t!at is' one
transaction t!at is confined to a single' specific enterprise database.
T!e application sends a re+uest for data to t!e JDBC driver' w!ic! t!en translates t!e
re+uest and sends it across t!e network to t!e database. T!e database returns t!e
data to t!e driver' w!ic! t!en translates t!e result to t!e application' as illustrated in t!e
following diagram(
T!is e*ample illustrates t!e basic flow of information in a simplified system3 !owever'
t!e enterprise of today uses application servers' w!ic! adds anot!er component to t!e
process.
Application Servers
T!e application server is anot!er component of t!e transaction process t!at is
addressed by t!e JTA. Application servers !andle t!e bulk of application operations and
take some of t!e load off of t!e end"user application. Building on t!e preceding
e*ample' we see t!at t!e application server adds anot!er process tier to t!e
transaction(
4p to t!is point' our e*amples illustrate a single' local transaction and describe four out
of t!e five components of t!e distributed transaction model. T!e fift! component' t!e
transaction manager' comes into consideration only w!en transactions are to be
distributed.
Understanding JTAthe Java Transaction API (cont!
Distrib"ted Transactions and the Transaction #anager
As we stated previously' a distributed transaction is a transaction t!at accesses and
updates data on two or more networked resources. T!ese resources could consist of
several different ,DB-)s !oused on a single sever' for e*ample' .racle' )/0 )erver'
and )ybase3 or t!ey could include several instances of a single type of database
residing on a number of different servers. $n any case' a distributed transaction involves
coordination among t!e various resource managers. T!is coordination is t!e function of
t!e transaction manager.
T!e transaction manager is responsible for making t!e final decision eit!er to commit or
rollback any distributed transaction. A commit decision s!ould lead to a successful
transaction3 rollback leaves t!e data in t!e database unaltered. JTA specifies standard
Java interfaces between t!e transaction manager and t!e ot!er components in a
distributed transaction( t!e application' t!e application server' and t!e resource
managers. T!is relations!ip is illustrated in t!e following diagram(
T!e numbered bo*es around t!e transaction manager correspond to t!e t!ree interface
portions of JTA(
54serTransactionT!e 6ava*.transaction.4serTransaction interface provides t!e
application t!e ability to control transaction boundaries programmatically. T!e
6ava*.transaction.4serTransaction met!od starts a global transaction and associates
t!e transaction wit! t!e calling t!read.
7Transaction -anagerT!e 6ava*.transaction.Transaction-anager interface allows
t!e application server to control transaction boundaries on be!alf of t!e application
being managed.
89A,esourceT!e 6ava*.transaction.*a.9A,esource interface is a Java mapping of
t!e industry standard 9A interface based on t!e 9:.pen CA; )pecification %Distributed
Transaction #rocessing( T!e 9A )pecification&.
<otice t!at a critical link is support of t!e 9A,esource interface by t!e JDBC driver.
T!e JDBC driver must support bot! normal JDBC interactions' t!roug! t!e application
and:or t!e application server' as well as t!e 9A,esource portion of JTA.
Developers of code at t!e application level s!ould not be concerned about t!e details
of distributed transaction management. T!is is t!e 6ob of t!e distributed transaction
infrastructuret!e application server' t!e transaction manager' and t!e JDBC driver.
T!e only caveat for application code is t!at is s!ould not invoke a met!od t!at would
affect t!e boundaries of a transaction w!ile t!e connection is in t!e scope of a
distributed transaction. )pecifically' an application s!ould not call t!e Connection
met!ods commit' rollback' and setAutoCommit%true& because t!ey would interfere wit!
t!e infrastructures management of t!e distributed transaction.
The Distrib"ted Transaction Process
T!e transaction manager is t!e primary component of t!e distributed transaction
infrastructure3 !owever' t!e JDBC driver and application server components s!ould
!ave t!e following c!aracteristics(
T!e driver s!ould implement t!e JDBC 7.= A#$' including t!e .ptional #ackage
interfaces 9AData)ource and 9AConnection' and t!e JTA interface
9A,esource.
T!e application server s!ould provide a Data)ource class t!at is implemented to
interact wit! t!e distributed transaction infrastructure and a connection pooling
module %for improved performance&.
T!e first step of t!e distributed transaction process is for t!e application to send a
re+uest for t!e transaction to t!e transaction manager. Alt!oug! t!e final
commit:rollback decision treats t!e transaction as a single logical unit' t!ere can be
many transaction branches involved. A transaction branc! is associated wit! a re+uest
to eac! resource manager involved in t!e distributed transaction. ,e+uests to t!ree
different ,DB-)s' t!erefore' re+uire t!ree transaction branc!es. ;ac! transaction
branc! must be committed or rolled back by t!e local resource manager. T!e
transaction manager controls t!e boundaries of t!e transaction and is responsible for
t!e final decision as to w!et!er or not t!e total transaction s!ould commit or rollback.
T!is decision is made in two p!ases' called t!e Two"#!ase Commit #rotocol.
$n t!e first p!ase' t!e transaction manager polls all of t!e resource managers
%,DB-)s& involved in t!e distributed transaction to see if eac! one is ready to commit.
$f a resource manager cannot commit' it responds negatively and rolls back its
particular part of t!e transaction so t!at data is not altered.
$n t!e second p!ase' t!e transaction manager determines if any of t!e resource
managers !ave responded negatively' and' if so' rolls back t!e w!ole transaction. $f
t!ere are no negative responses' t!e translation manager commits t!e w!ole
transaction' and returns t!e results to t!e application.
Developers of transaction manager code must be conversant wit! all t!ree interfaces of
JTA( 4serTransaction' Transaction-anager' and 9A,esource' w!ic! are described in
t!e )un JTA specification. T!e JDBC API Tutorial and Reference, Second Edition is
also a useful reference. JDBC driver developers need only be concerned wit! t!e
9A,esource interface. T!is interface is a Java mapping of t!e industry standard
9:.pen 9A protocol t!at allows a resource manager to participate in a transaction. T!e
component of t!e driver connected wit! t!e 9A,esource interface is responsible for
1translating1 between t!e transaction manager and t!e resource manager. T!e
following section provides e*amples of 9A,esource calls.
The JD$C Driver and %A&eso"rce
To simplify t!e e*planation of 9A,esource' t!ese e*amples illustrate !ow an
application would use JTA w!en t!ere is no application server and transaction manager
involved. Basically' t!e application in t!ese e*amples is also acting as application
server and transaction manager. -ost enterprises use transaction managers and
application servers because t!ey manage distributed transactions muc! more efficiently
t!an an application can. By following t!ese e*amples' !owever' an application
developer can test t!e robustness of JTA support in a JDBC driver. )ome e*amples
may not work for a particular database because of in!erent problems associated wit!
t!at database.
Before using JTA' you must first implement an 9id class for identifying transactions %t!is
would normally be done by t!e transaction manager&. T!e 9id contains t!ree elements(
format$D' gtrid %global transaction $D&' and b+ual %branc! +ualifier $D&.
T!e format$D is usually >ero' meaning t!at you are using t!e .)$ CC, %.pen )ystems
$nterconnection Commitment' Concurrency' and ,ecovery standard& for naming. $f you
are using anot!er format' t!e format$D s!ould be greater t!an >ero. A value of "5
means t!at t!e 9id is null.
T!e gtrid and b+ual can eac! contain up to ?@ bytes of binary code to identify t!e
global transaction and t!e branc! transaction' respectively. T!e only re+uirement is t!at
t!e gtrid and b+ual taken toget!er must be globally uni+ue. Again' t!is can be ac!ieved
by using t!e naming rules specified in t!e .)$ CC,.
T!e following e*ample illustrates implementation of an 9id(
import javax.transaction.xa.*;
public class MyXid implements Xid
{
protected int formatId;
protected byte gtrid[];
protected byte bual[];
public MyXid!"
{
#
public MyXid!int formatId$ byte gtrid[]$ byte bual[]"
{
t%is.formatId & formatId;
t%is.gtrid & gtrid;
t%is.bual & bual;
#
public int get'ormatId!"
{
return formatId;
#
public byte[] get(ranc%)ualifier!"
{
return bual;
#
public byte[] get*lobal+ransactionId!"
{
return gtrid;
#
#
)econd' you need to create a datasource for t!e database t!at you are using(
public ,ata-ource get,ata-ource!"
t%ro.s -)/0xception
{
-)/-erver,ata-ource xa,- & ne.
com.merant.datadirect.jdbcx.slserver.-)/-erver,ata-ource!";
xa,-.set,ata-ource1ame!2-)/-erver2";
xa,-.set-erver1ame!2server2";
xa,-.set3ort1umber!4566";
xa,-.set-electMet%od!2cursor2";
return xa,-;
#
'(ample )T!is e*ample uses t!e two"p!ase commit protocol to commit one
transaction branc!(
X7,ata-ource xa,-;
X78onnection xa8on;
X79esource xa9es;
Xid xid;
8onnection con;
-tatement stmt;
int ret;
xa,- & get,ata-ource!";
xa8on & xa,-.getX78onnection!2jdbc:user2$ 2jdbc:pass.ord2";
xa9es & xa8on.getX79esource!";
con & xa8on.get8onnection!";
stmt & con.create-tatement!";
xid & ne. MyXid!4;;$ ne. byte[]{;x;4#$ ne. byte[]{;x;<#";
try {
xa9es.start!xid$ X79esource.+M1='/7*-";
stmt.execute>pdate!2insert into test:table values !4;;"2";
xa9es.end!xid$ X79esource.+M->880--";
ret & xa9es.prepare!xid";
if !ret && X79esource.X7:=?" {
xa9es.commit!xid$ false";
#
#
catc% !X70xception e" {
e.print-tac@+race!";
#
finally {
stmt.close!";
con.close!";
xa8on.close!";
#
Because t!e initiali>ation code is t!e same or very similar for all t!e e*amples' only
significantly different code is represented from t!is point forward.
'(ample *T!is e*ample' similar to ;*ample 5' illustrates a rollback(
xa9es.start!xid$ X79esource.+M1='/7*-";
stmt.execute>pdate!2insert into test:table values !4;;"2";
xa9es.end!xid$ X79esource.+M->880--";
ret & xa9es.prepare!xid";
if !ret && X79esource.X7:=?" {
xa9es.rollbac@!xid";
#
'(ample +T!is e*ample s!ows !ow a distributed transaction branc! suspends' lets
t!e same connection do a local transaction' and t!em resumes t!e branc! later. T!e
two"p!ase commit actions of distributed transaction do not affect t!e local transaction.
xid & ne. MyXid!4;;$ ne. byte[]{;x;4#$ ne. byte[]{;x;<#";
xa9es.start!xid$ X79esource.+M1='/7*-";
stmt.execute>pdate!2insert into test:table values !4;;"2";
xa9es.end!xid$ X79esource.+M->-301,";
AA +%is update is done outside of transaction scope$ so it
AA is not affected by t%e X7 rollbac@.
stmt.execute>pdate!2insert into test:table< values !444"2";
xa9es.start!xid$ X79esource.+M90->M0";
stmt.execute>pdate!2insert into test:table values !<;;"2";
xa9es.end!xid$ X79esource.+M->880--";
ret & xa9es.prepare!xid";
if !ret && X79esource.X7:=?" {
xa9es.rollbac@!xid";
#
'(ample ,T!is e*ample illustrates !ow one 9A resource can be s!ared among
different transactions. Two transaction branc!es are created' but t!ey do not belong to
t!e same distributed transaction. JTA allows t!e 9A resource to do a two"p!ase commit
on t!e first branc! even t!oug! t!e resource is still associated wit! t!e second branc!.
xid4 & ne. MyXid!4;;$ ne. byte[]{;x;4#$ ne. byte[]{;x;<#";
xid< & ne. MyXid!4;;$ ne. byte[]{;x44#$ ne. byte[]{;x<<#";
xa9es.start!xid4$ X79esource.+M1='/7*-";
stmt.execute>pdate!2insert into test:table4 values !4;;"2";
xa9es.end!xid4$ X79esource.+M->880--";
xa9es.start!xid<$ X79esource.+M1='/7*-";
AA -%ould allo. X7 resource to do t.oBp%ase commit on
AA transaction 4 .%ile associated to transaction <
ret & xa9es.prepare!xid4";
if !ret && X79esource.X7:=?" {
xa9es.commit!xid<$ false";
#
stmt.execute>pdate!2insert into test:table< values !<;;"2";
xa9es.end!xid<$ X79esource.+M->880--";
ret & xa9es.prepare!xid<";
if !ret && X79esource.X7:=?" {
xa9es.rollbac@!xid<";
#
'(ample -T!is e*ample illustrates !ow transaction branc!es on different
connections can be 6oined as a single branc! if t!ey are connected to t!e same
resource manager. T!is feature improves distributed transaction efficiency because it
reduces t!e number of two"p!ase commit processes. Two 9A connections to t!e same
database server are created. ;ac! connection creates its own 9A resource' regular
JDBC connection' and statement. Before t!e second 9A resource starts a transaction
branc!' it c!ecks to see if it uses t!e same resource manager as t!e first 9A resource
uses. $f t!is is case' as in t!is e*ample' it 6oins t!e first branc! created on t!e first 9A
connection instead of creating a new branc!. 0ater' t!e transaction branc! can be
prepared and committed using eit!er 9A resource.
xa,- & get,ata-ource!";
xa8on4 & xa,-.getX78onnection!2jdbc:user2$ 2jdbc:pass.ord2";
xa9es4 & xa8on4.getX79esource!";
con4 & xa8on4.get8onnection!";
stmt4 & con4.create-tatement!";
xid4 & ne. MyXid!4;;$ ne. byte[]{;x;4#$ ne. byte[]{;x;<#";
xa9es4.start!xid4$ X79esource.+M1='/7*-";
stmt4.execute>pdate!2insert into test:table4 values !4;;"2";
xa9es4.end!xid$ X79esource.+M->880--";
xa8on< & xa,-.getX78onnection!2jdbc:user2$ 2jdbc:pass.ord2";
xa9es< & xa8on4.getX79esource!";
con< & xa8on4.get8onnection!";
stmt< & con4.create-tatement!";
if !xa9es<.is-ame9M!xa9es4"" {
xa9es<.start!xid4$ X79esource.+MC=I1";
stmt<.execute>pdate!2insert into test:table< values !4;;"2";
xa9es<.end!xid4$ X79esource.+M->880--";
#
else {
xid< & ne. MyXid!4;;$ ne. byte[]{;x;4#$ ne. byte[]{;x;6#";
xa9es<.start!xid<$ X79esource.+M1='/7*-";
stmt<.execute>pdate!2insert into test:table< values !4;;"2";
xa9es<.end!xid<$ X79esource.+M->880--";
ret & xa9es<.prepare!xid<";
if !ret && X79esource.X7:=?" {
xa9es<.commit!xid<$ false";
#
#
ret & xa9es4.prepare!xid4";
if !ret && X79esource.X7:=?" {
xa9es4.commit!xid4$ false";
#
'(ample .T!is e*ample s!ows !ow to recover prepared or !euristically completed
transaction branc!es during failure recovery. $t first tries to rollback eac! branc!3 if it
fails' it tries to tell resource manager to discard knowledge about t!e transaction.
MyXid[] xids;
xids & xa9es.recover!X79esource.+M-+79+9-871 D X79esource.+M01,9-871";
for !int i&;; xidsE&null FF iGxids.lengt%; iHH" {
try {
xa9es.rollbac@!xids[i]";
#
catc% !X70xception ex" {
try {
xa9es.forget!xids[i]";
#
catc% !X70xception ex4" {
-ystem.out.println!2rollbac@Aforget failedI 2 H ex4.error8ode";
#
#
#
&e/erences
C!eung A -atena' Jaa Transaction API !JTA"' 5BBB' )un -icrosystems' $nc.
C!ite' 2is!er' Cattell' Hamilton' Hapner' and )un -icrosystems' $nc.' JDBC API
Tutorial and Reference, Second Edition' 5BBB' Addison"Cesley.
#$%pen CAE Specification, Distributed Transaction Processing& The #A Specification'
5BB5' T!e 9:.pen Company.

Das könnte Ihnen auch gefallen