Learn how to share transactions and locks, avoid orphaned transactions, and more.
In my
20+ years experience in the software development field, I've met only a few
(lucky) system architects who had the luxury to design new software solutions
from scratch. More often, a system architect faces a challenge of integrating
existing software components developed in legacy programming languages with new
components written in modern languages such as Java. For complex applications,
the transition from legacy code base to Java may take months if not years. In
addition, rewriting existing software components may be impractical or
impossible. In this case, a business process implementation can be split between
legacy code and Java. For example, you may wish to retain a robust order
processing module and augment it with a Java module that allows the customers to
place orders over the Internet. Both modules work on the same transactional
resources (customer, order, and stock data) and ideally should be able to share
the same transaction context. In this article, I present a multithreaded
framework that allows for concurrent execution of Java and legacy components
that access and manipulate shared database resources.
The Distributed Transaction 101
The improved transaction processing architecture,
implemented back in V5R2, allows i5/OS to fully and efficiently support the
transaction model represented by the X/Open Distributed Transaction
Specification (DTS) and the Java JTA specification. The cornerstone of this
improved architecture, sometimes referred to as New Transaction Services (NTS),
is the decoupling of transactions from threads, processes, and activation
groups. An internal MI object, a transaction object, has been added to
facilitate the separation of a transaction context from a given activation
group.
In the NTS, the locks and commit blocks are scoped to a
transaction object. The associated space of a Transaction Object contains the
commitment definition for the transaction.
The association of a
transaction object to an XA thread of control is temporary. To accommodate both
X/Open DTP and JTA specifications, i5/OS implements the concept of the XA thread
of control in two ways:
- A system thread acts as the XA thread of control.
This is the default behavior. When a global transaction represented by its
global transaction identifier (XID) is activated—for example by the
xa_start(XID) function—the transaction handle (a unique identifier) and
the associated transaction object's address are set in the calling thread's
Thread Control Block (TCB). After the thread has been associated with a
transaction, each SQL statement processed in that thread uses the transaction
context associated with that thread. The thread can be disassociated from the
transaction by calling the xa_end(XID) function. At this time, the transaction
information in the Thread Control Block is set to NULL.
- In V5R4, a second method was formalized in which the SQL connection acts as
the XA thread of control. The application servers implementing JTA—such as
WebSphere, as well as clients that call XA through ZDA, DRDA, or XDA
servers—use this connection thread of control model. When using connection
as the thread of control natively on i5, transaction associations are started
and ended via the SqlSetConnectAttr CLI API rather than the xa_start and xa_end
APIs.
These concepts are illustrated in Figure 1.
Figure 1: The transaction is decoupled from an
XA thread of control. (Click images to enlarge.)
According to the X/Open DTS specification,
only one thread can be associated with a transaction context at any given time.
This means that multiple threads can work on the same transaction, but this work
needs to be multiplexed. In some environments, this may fulfill the
requirements. For example, a Java client attaches to a new global transaction,
performs some transactional work, and detaches from the transaction. The
transaction is then passed to a legacy program that attaches to this
transaction, performs some additional transactional work, and detaches from the
transaction. At this point, a transaction monitor may decide to complete the
transaction. This works fine since the locks and the commitment definition are
scoped to a given transaction object. In the high volume transactional systems,
however, there are typically multiple threads that wish to manipulate
overlapping sets of resources.
The simple multiplexing scenario
described above does not provide the required concurrency levels. The threads
need to wait for their turn to attach to the transaction or spawn another
transaction, which often results in lock conflicts and deadlocks. These issues
are addressed by loosely coupled transactions that share locks.
Huh? OK,
let me explain. The global transactions are said to be "loosely coupled" when
the transaction identifiers (XIDs) of two transaction branches have the same
global transaction identifier (GTRID) but different branch qualifiers (BQUALs).
By default, loosely coupled transaction branches do not share locks. On DB2 for
i5/OS, this means that there are two separate transaction objects for these two
branches. Starting with V5R4, however, there is an option that allows loosely
coupled transactions to share locks. In other words, multiple loosely coupled
branches can now share the same transaction object. In this case, each thread
can have its own transaction branch within a resource manager. Multiple threads
can continue transactional work on the same resources without ever running into
lock conflicts.
Generally, a transaction branch
can be in one of the five states: Active, Idle, Prepared, Rollback Only, and
Heuristically Completed. The allowable state transitions are defined in Table
6-4 in Distributed
Transaction Processing: The XA Specification. Note that the lock-sharing
functionality for loosely coupled transaction branches is not architected by XA.
Rather, it is an extension to the spec. In DB2 implementation, the last
transaction branch to be completed commits or rolls back the changes for all the
transaction branches with the same GTRID. The xa_prepare requests for the other
transaction branches complete those transaction branches and return XA_RDONLY.
However, changes made and locks acquired for those transaction branches remain
pending until the last transaction branch with the same GTRID runs to
completion. If xa_rollback is requested for one transaction branch while others
are not yet completed, DB2 handles the request by marking the other branches
rollback-only. So it is not possible for some branches to commit while others
roll back. The xa_commit requests for other transaction branches receive a
XA_ROLLBACK return code if xa_rollback was already requested for one or more of
the loosely coupled transaction branches that share locks. Note also that it is
not valid to request xa_rollback for one of the transaction branches before all
are prepared because the transaction manger must carry out a two-phase commit if
there are multiple transaction branches.
Sample Application Walkthrough
I will illustrate how to take advantage of the
loosely coupled transactions that share locks using a sample application. The
application consists of several major components:
- JTAMonitor.java—This
is a Java class that uses JTA to manage global transactions. It creates new
branches, passes them to other components, and completes them. It also contains
logic that can be used to complete orphaned transactions or query the status of
global transactions.
- JInsertCoffees.java—This is a Java class that inserts a new row into a
database table called COFFEES. The insert is performed in the context of an
existing global transaction that has been previously started by JTAMonitor.
- CUpdCoff.c—This is a native ILE C program that updates the row
previously inserted by JInsertCoffees. The update is performed in the context of
a global transaction started by JTAMonitor.
- CUpdateCoffeesWrapper.java—This is a wrapper class that uses Program
Call Markup Language (PCML) implemented in the IBM Toolbox for Java to call the
native CUpdCoff program from Java.
- TestXATransactions.java—This is the main Java program that calls
various components to perform transactional work.
The
application also contains a number of Java helper classes that are necessary to
perform such mundane tasks as configuring a DB2 for i5/OS data source, creating
new XID object, printing out the list of global transactions, and so forth.
You'll find a short description of those classes in the readme.1st file that is
contained in the downloadable
material that accompanies this article.
For illustration purposes,
the global transaction identifier (GTRID) and branch qualifier (BQUAL) are
passed to the software components as input parameters. In the test scenario, the
JTAMonitor starts three transaction branches with the same GTRID but different
BQUALs. Next, a row is inserted through Java. The C program is executed twice,
each time within a different transaction branch. So the row is updated two
times. If all components execute successfully, JTAMonitor commits the
transaction. Otherwise, a rollback is requested.
Let's examine the source
code to see how these tasks get accomplished. Here's a code excerpt from
TestXATransactions.java (most of the error-handling was removed for
clarity):
public static void main(String[] args) {
CoffeeBean newCoffee = new CoffeeBean(10, "Colombian Select",
150, (float) 9.95, 0); [1]
CoffeeBean updtCoffee1 = new CoffeeBean(10, 100); [2]
CoffeeBean updtCoffee2 = new CoffeeBean(10, 200);
AS400JDBCXADataSource ds1 = new AS400JDBCXADataSource(); [3]
XADataSourceConfigurator.configAS400JDBCXADataSource(ds1); [4]
JTAMonitor monitor = new JTAMonitor(); [5]
ExecutorService pool = Executors.newFixedThreadPool(3); [6]
...
try {
monitor.setXads(ds1); [7]
monitor.beginTransaction("0100000000000000","0000000000000000" ); [8]
monitor.beginTransaction("0100000000000000","0100000000000000" );
monitor.beginTransaction("0100000000000000","0200000000000000" );
JInsertCoffees insCof = new JInsertCoffees(0,
"0100000000000000","0000000000000000",newCoffee); [9]
insCof.setXads(ds1);
Future t0 = pool.submit(insCof); [10]
try {
TrnsWorkResult twResult = t0.get(); [11]
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (ExecutionException ex) {
ex.printStackTrace();
TrnsMonitorException tmex = (TrnsMonitorException) ex.getCause();[12]
throw tmex; [13]
}
Future t1 =
pool.submit(new CUpdateCoffeesWrapper(1,
"0100000000000000","0100000000000000",updtCoffee1)); [14]
Future t2 =
pool.submit(new CUpdateCoffeesWrapper(2,
"0100000000000000","0200000000000000",updtCoffee2)); [15]
try {
TrnsWorkResult twResult = t1.get();
twResult = t2.get();
} catch (InterruptedException ex) {
} catch (ExecutionException ex) { ...}
monitor.prepareTransaction("0100000000000000","0000000000000000" );[16]
monitor.prepareTransaction("0100000000000000","0100000000000000" );
monitor.prepareTransaction("0100000000000000","0200000000000000" );
try {
monitor.commitTransaction("0100000000000000","0000000000000000" );[17]
System.out.println("bqual0 - Commit successful.");
} catch (TrnsMonitorException tmex) {...}
try {
monitor.commitTransaction("0100000000000000","0100000000000000" );
} catch (TrnsMonitorException tmex) {...}
try {
monitor.commitTransaction("0100000000000000","0200000000000000" );
} catch (TrnsMonitorException tmex) {...}
} catch (TrnsMonitorException tmex) {
System.out.println("Error occured in JTAMonitor.");
try {
monitor.rollbackTransaction(
"0100000000000000","0000000000000000" ); [18]
monitor.rollbackTransaction(
"0100000000000000","0100000000000000" );
monitor.rollbackTransaction(
"0100000000000000","0200000000000000" );
} catch (TrnsMonitorException tmex2) {
...
try {
TransactionList.
printTransactionList(monitor.
retrieveTransactionList()); [19]
} catch (TrnsMonitorException tmex3) {...}
}
}
} |
|
Figure 2: This is the source code of
TestXATransactions.java.
In Figure 2, at [1] a CoffeeBean is
instantiated. This bean contains the information about a new coffee brand to be
inserted into the COFFEES table. At [2], another CoffeeBean is instantiated.
This time, I use a constructor that accepts just two parameters: cof_id, and
sales. This bean is later used to update an existing row in the table. The
cof_id is used to locate the row in the table and sales to update the SALES
column. The AS400JDBCXADataSource object is created at [3]. This object is
contained in the IBM Toolbox for Java, and it implements the XADataSource
interface. The data source is configured at [4]. The XADataSourceConfigurator
helper class sets the data source's properties—such as system name, user
ID, and user password—necessary to connect to System i. In addition, it
sets the XALooselyCoupledSupport property to 1 to enable the loosely coupled
transactions that share locks. This is shown in the following code
snippet:
ds1.setXALooselyCoupledSupport(1);
The data source is used by other software components to produce
XAConnection and XAResource objects. At [5], the JTAMonitor is created. As
mentioned, this class is responsible for the distributed transaction management.
I discuss the JTAMonitor implementation in the section below.
The main
purpose of the sample application is to illustrate how to perform concurrent
transactional work on the same set of resources. This requires that multiple
threads are created and executed in parallel. To implement this behavior, I use
a new concurrency framework that has been introduced in Java 1.5, which is based
on ExecutorService, Callable, and Future interfaces. An ExecutorService object
executes submitted Callable tasks. It contains a pool of worker threads that run
the tasks concurrently and asynchronously. A Callable task implements the
Callable interface, which is similar to Runnable in that both are designed for
classes whose instances can be executed by another thread. A Callable, however,
returns a result and may throw an exception. The result and the possible
exception returned from a Callable are encapsulated in a Future object that is
produced by the ExecutorService upon return from a worker thread. So, at [6] an
ExecutorService object is instantiated, and its worker thread pool size is set
to 3. At [7] the reference to the previously configured data source object is
passed to the JTAMonitor. The beginTransaction method is called on the
JTAMonitor at [8] to start the first branch of the global transaction. Then, two
other branches are also started. Note that all three transaction branches have
the same transaction identifier (GTRID) but different branch qualifiers
(BQUALs). At [9] an JInsertCoffees object is created. Since this class
implements Callable, it can be submitted to ExecutorService for execution at
[10]. The result is returned at [11] by calling the get method on the Future
object. If an exception is thrown in JInsertCoffees, it gets encapsulated in an
ExecutionException and returned to caller. The original exception can be
extracted by calling getCause method on the ExecutionException object as shown
at [12]. The original exception that can be thrown in JInsertCoffees is actually
a custom exception called TrnsMonitorException. It is a wrapper that allows me
to return XA, SQL, and application-specific exceptions. It is re-thrown at [13]
to force a rollback. At [14] and [15] I use the same pattern as at [10]. This
time, however, two update requests are submitted concurrently. Note that both
updates attempt to modify the same row (cof_id = 10). The loosely coupled
support guarantees that there are no locking conflicts. In the very unlikely
case where two threads attempted the update at the very same instance of time,
the access conflict would be resolved by DB2 through the internal seizes. The
rest of the TestXATransactions flow is pretty straightforward. If the control
reaches [16], the insert and two updates must have succeeded, so all three
branches are prepared. Then at [17] a commit is attempted. The commit needs to
be in the try-catch block, because two branches are completed at the prepare
time. In the DB2 for i5/OS implementation, all three transaction branches share
the same transaction object. Two branches are marked as read-only, and only one
branch is marked as read-write. According to the XA DTP spec, the read-only
branches get completed at the prepare time (since, by definition, there is
nothing to commit). So, all transactional work is indirectly (through the
associated transaction object) scoped to the read-write transaction branch. Only
this transaction branch can be committed. In a typical scenario, where there is
a pool of loosely coupled transactions, the last transaction that was prepared
can also be committed. At this point, the transactional work represented by the
transaction object is committed, the locks released, and the object purged from
the system. If errors occur during the TestXATransactions execution the program
attempts to gracefully complete the outstanding branches, as shown at [18].
Should the rollback fail, the list of existing transactions is printed to the
console at [19] so that the administrator can take an appropriate
action.
Now that the flow of the sample application was outlined, let's
focus on the most critical coding techniques used in various
components:
JTAMonitor implements methods to start, prepare, commit, and
roll back transaction branches. All these methods follow a very similar logic.
For example, here's an excerpt from beginTransaction method:
public void beginTransaction(String globalID, String branchID) throws TrnsMonitorException { TrnsMonitorException tmex = null; try { Xid xid = new XidImplQZDA(globalID, branchID); [1] XAConnection xaConn1 = getXads().getXAConnection(); [2] XAResource xaRes1 = xaConn1.getXAResource(); [3] try { xaRes1.start(xid, XAResource.TMNOFLAGS); [4] } catch (XAException xae) {...} try { xaRes1.end(xid, XAResource.TMSUCCESS); [5] } catch (XAException xae) {...} } catch (Exception ex) {...} } |
|
Figure 3: This is the beginTransaction method
implemented in JTAMonitor.java.
In Figure 3, at [1] an Xid object is
instantiated. This object represents the transaction context and is used by the
resource manager (DB2, in this case) to identify a specific global transaction.
At [2] the XADataSource produces an XAConnection. In a DB2 for i5/OS
implementation, the transactional work submitted over a given connection is
actually executed by one of the database prestart jobs. Since the sample
application utilizes the IBM Toolbox for Java JDBC driver, the database
connections are served by QZDASOINIT jobs. At [3] the XAConnection, in turn,
produces an XAResource object. At [4] the newly created XAResource object is
used to start a new global transaction with GTRID and BQUAL values encapsulated
in the Xid object. The start method not only creates a new transaction context
on the resource manager but also automatically associates the current connection
with the transaction context. The transaction state is changed to active. The
beginTransaction method performs no transactional work. Its purpose is to
initiate a new transaction branch. The transactional work is performed by other
specialized classes such as JInsertCoffees. So at this point, I disassociate the
current thread from the transaction context. This is accomplished at [5]. The
transaction status changes from active to idle. For example, JInsertCoffees uses
the transaction branch started by the beginTransaction method to insert a new
row into the COFFEES table. Here's a code fragment that represents the core
functionality of the JInsertCoffess class:
try { Xid xid = new XidImplQZDA(globalID, branchID); [1] XAConnection xaConn1 = xads.getXAConnection(); [2] XAResource xaRes1 = xaConn1.getXAResource(); [3] try{ xaRes1.start(xid, XAResource.TMJOIN); [4] } catch (XAException xae) {...} c1 = xaConn1.getConnection(); [5] String sqlinstr = "INSERT INTO coffees VALUES(?,?,?,?,?,?)"; pstmt = c1.prepareStatement(sqlinstr); [6] pstmt.setInt(1, aCoffee.getCof_id()); ... int count = pstmt.executeUpdate(); [7] try{ xaRes1.end(xid, XAResource.TMSUCCESS); [8] } catch (XAException xae) {...} } catch (Exception e) {...} |
|
Figure 4: This pseudo code illustrates the
core functionality of JInsertCoffees.
In Figure
4, at [1] the Xid implementation object is created. At [2] the XADataSource
produces an XAConnection. The necessary XAResource object is instantiated at
[3]. This is a pattern that you'll see throughout the sample application. It
allows me to make the classes that manipulate the data resource manager
independent. At [4] the XAResource is used to associate the transactional work
performed by JInsertCoffees with a transaction context represented by a given
Xid. Remember that at this point the transaction branch status is changed from
Idle to Active. At [5] a logical database connection is obtained from
XAConnection; an SQL INSERT statement is prepared at [6] and executed at [7].
Finally, at [8] the thread is disassociated from the branch so that other
threads can gain control over it.
A similar logic is employed in the
legacy CUPDCOF program. In this case, however, I use the XA APIs implemented in
C rather than JTA APIs in Java. Let's quickly examine the relevant excerpt from
the C source:
#define FORMATID
4478019 [1]
char xa_info[1024]=
"tmname=TMJAREK rdbname=*LOCAL tblcs=S " ; [2]
extern struct
xa_switch_t xa_switch; [3]
int main(int argc,
char *argv[]) {
rmid =
1;
flags =
TMNOFLAGS;
xaRC =
xa_switch.xa_open_entry(xa_info, rmid, TMNOFLAGS); [4]
vParam =
SQL_TRUE;
cliRC =
SQLSetConnectAttr(hdbc,
SQL_ATTR_TXN_EXTERNAL, (SQLPOINTER)
&vParam,
0);
[5]
cliRC=SQLConnect
(hdbc, server, SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS)
xid =
getXid(gtrid, bqual);
[6]
xaRC =
xa_switch.xa_start_entry(xid, rmid, TMJOIN);
[7]
SQLCHAR sqlstmt[]
= "UPDATE coffees SET sales = sales + ?,
total = total + price
* ? WHERE cof_id =
?";
cliRC=SQLPrepare
(hstmt, sqlstmt, SQL_NTS); [8]
cliRC =
SQLExecute (hstmt);
[9]
cliRC =
SQLDisconnect (hdbc);
xaRC =
xa_switch.xa_end_entry(xid, rmid, TMSUCCESS);
[10]
}
} |
|
Figure 5: This pseudo code illustrates the
core functionality of CUpdCof.
In Figure 5, at [1] a format ID is
defined. This value is used later to set up the xid structure. The format ID for
various transaction branches that wish to share locks needs to be the same. In
the sample application, both the Java helper class XidImplQZDA and the C helper
function called in CUpdCof set the format ID to the same value. At [2] the
xa_info variable is defined. The xa_info is a null-terminated string that
contains resource-manager-specific information. I specify three keywords in the
xa_info string:
- tmname—This is the transaction monitor name, in
this case an arbitrary value.
- rdbname—This is the name identifying the relational database used for
XA transaction branches. Since the program runs natively on i5/OS, I use the
special value *LOCAL to indicate the local database instance.
- tblcs—This is the value that controls the behavior for loosely coupled
transactions. "S" means that I want them to share locks. The resource access
conflicts do not occur between the transaction branches.
At [3]
the xa_switch_t structure is defined. This switch, which is provided by DB2,
gives access to the XA routines such as xa_open_entry, xa_start_entry, and so
forth. Using this switch, I call the xa_open_entry function at [4] to initialize
and prepare the DB2 runtime for distributed transaction processing. At [5] the
SQL_ATTR_TXN_EXTERNAL connection attribute is set to SQL_TRUE to indicate the
use of XA transactions rather than local transactions. At [6] the getXid
function is called to set up the xid structure. As mentioned, the two necessary
parameters (gtrid, bqual ) are passed to the C program from the
JUpdateCoffeesWrapper class via a PCML call. The transaction branch represented
by these parameters has been already started by JTAMonitor. At [7] the
xa_start_entry function is called to join that branch. An SQL UPDATE statement
is prepared at [8] and executed at [9]. Finally, at [10] the thread is
disassociated from the branch.
Note that I could implement the update
coffee logic in RPG or COBOL. The only requirement is that the transactional
work must be performed through SQL.
Monitoring and Managing Global Transactions
Generally there are two approaches to managing and
monitoring global transactions:
- Use utilities provided by i5/OS.
- Use JTA, XA, and system APIs in a custom transaction
monitor.
The programmatic approach ensures the highest level of
flexibility but requires development and maintenance investment. So, first I
cover the tools available in the powerful i5/OS environment.
i5/OS Utilities
A System i administrator may have to deal with
global transactions initiated by various software components, sometimes running
on remote systems. The i5/OS provides a number of utilities that can be used to
manage global transactions. Generally, these utilities can be accessed through
one of the following interfaces:
Green-screen 5250 session—The Work
with Commitment Definitions (WRKCMTDFN) CL command can be used to display
commitment definitions associated with XA transactions. Here's how to call this
command from the i5/OS prompt:
WRKCMTDFN JOB(*ALL) STATUS(*XOPEN)
iSeries Navigator GUI—The Global Transactions dialog shows
the list of global transactions on a given database. This is illustrated in
Figure 6:
Figure 6: This is the Global Transactions
dialog.
You can right-click any of the transactions listed in the right
panel to display a context menu. For example, select Properties from the context
menu to display the various attributes of a given transaction. On the Properties
dialog, click the X/Open tab to display the XA specific attributes. This is
shown in Figure 7 below:
Figure 7: These are the distributed
transaction attributes.
Note that for loosely coupled transactions that
share locks, the Lock sharing field in the Properties dialog should read
Yes.
By selecting the Locked Objects option from the transaction's
context menu, you can see the list of the database objects currently locked on
behalf of a given transaction. To drill down to the row-level locks, right-click
an object on the list—for example, the COFFEES table—and select
Locked Member. In the list that appears, select the entry with Member Lock Type
being Data and right-click it. Select Locked Rows. The dialog shown in Figure 8
appears:
Figure 8: Row-level locks are scoped to a
transaction.
Notice how the row lock is scoped to a transaction object
(represented by Lock Space ID) rather than any specific job. Also, there is just
one transaction object for all three branches.
Sometimes, a situation may
occur in which an application starts and prepares a transaction but, due to,
say, a communications failure, never completes it. Since DB2 did not receive the
second-phase message (commit or rollback), the resources are blocked. The system
administrator can unilaterally make a decision to complete the transaction and
thus release the locks. Keep in mind, however, that such a decision may lead to
heuristic outcomes. One option to force the completion of such a transaction is
to use the iSeries Navigator. In the Global Transactions panel, right-click the
transaction in question and select Force Commit or Force Rollback. These options
are available only for transactions that are in the Prepared status. In all
other cases, the options are grayed out.
So what can be done, if an
"orphaned" transaction is in a state other than Prepared or Heuristically
Completed and it blocks resources? Unfortunately, there is no tool on i5/OS to
remove an orphaned transaction object. This is so because the orphaned
transactions don't have any adverse effect unless they hold locks, or, for some
reason, you want to reuse the XID. Note that the WRKCMTDFN CL command's option
Forced Rollback has been enhanced in V5R4 to be allowed for non-prepared XA
transactions. However, you should not use this option as a normal cleanup
mechanism for non-Prepared transactions. It's intended for use in non-production
(test/development) environment.
Programmatic Approach
The sample application described in the previous
section will properly handle all XA errors and warnings. In fact, the JTAMonitor
class has methods to cope with unexpected outcomes. For example, the
cleanupTransaction method uses the xa_recover mechanism to clean up the
transactions that are in Prepared or Heuristically Completed state.
At
this point, a careful reader might wonder, "How can I programmatically monitor
the current status of global transactions?" The JTA API provides no interface to
retrieve the complete list of transactions, so the sample application uses an
undocumented i5/OS API for that purpose. You can request the API documentation
through your IBM Service Representative. The interesting tidbit is that the
iSeries Navigator GUI uses the same mechanism to retrieve the transaction list
shown in Figure 9. Consider the following excerpt from the
JTAMonitor.retrieveTransactionList method:
con = this.getXads().getXAConnection().getConnection();
cstm = con.
prepareCall("CALL QSYS.QTNOUTCD('QAGLTXLST QTEMP ','','CFMT0500','1', 0)");
cstm.executeUpdate(); [1]
con.commit();
pstm = con.prepareStatement("SELECT QTNGTXID," +
"QTNBQUAL, " +
"CASE QTNXIDSTT " +
"WHEN 'S1' THEN 'Active' " +
"WHEN 'S2' THEN 'Idle' " +
"WHEN 'S3' THEN 'Prepared' " +
"WHEN 'S4' THEN 'Rollback only' " +
"WHEN 'S5' THEN 'Heuristically completed' " +
"END, " +
"CASE QTNUOWSTT " +
"WHEN 'RST' THEN 'Reset' " +
"WHEN 'PIP' THEN 'Prepare in progress' " +
"WHEN 'PRP' THEN 'Prepared' " +
"WHEN 'LAP' THEN 'Last agent pending' " +
"WHEN 'CMT' THEN 'Committed' " +
"WHEN 'VRO' THEN 'Vote read-only' " +
"WHEN 'RBR' THEN 'Rollback required' " +
"WHEN 'RIP' THEN 'Rollback in progress' " +
"END," +
"QTNLCKSID " +
"FROM QTEMP.QAGLTXLST " +
"WHERE QTNASPGRP = '*SYSBAS' ORDER BY QTNGTXID, QTNBQUAL");
rs = pstm.executeQuery(); [2]
con.commit(); |
|
Figure 9: This is the core functionality of
the retrieveTransactionList method.
In Figure 9, at [1] a call to the
system stored procedure QSYS.QTNOUTCD is executed. Five parameters are passed to
the procedure:
- Outfile—Name and library of the file to receive the
output.
- RscOutfile—Name and library of the file to receive resource output.
Not required on this call.
- FilterFormat—Format of filter data. CFMT0500 means that transactions
managed by the manager specified in FilterData parameter are directed to the
output file.
- FilterData—Used to filter transaction context. There are two valid
values for CFMT0500: 0 for DB2 native transaction manager; 1 for "XA"-type
transaction manager.
- BinFilterData—Binary filter data. Not required on this
call.
At [2] a SELECT statement is executed to retrieve the
transaction list. The query returns several columns from the output file created
by the stored procedure call: global transaction ID, branch qualifier, branch
state, Unit of Work (UOW) state, and lock space ID (transaction object
ID).
The transaction list can be used, for example, to forget the transaction
branches that are in Heuristically Completed state.
An Attractive Alternative
The loosely coupled transactions that share locks
can be an attractive alternative for the software architectures where legacy and
modern software components need to perform transactional work on shared
resources. Before adopting the framework proposed in this article, however, make
sure that you understand the performance implications of using distributed
rather than local transaction model. In other words, the powerful distributed
transaction processing capabilities of i5/OS come at a price: a slightly
increased requirement for system capacity.
Additional Material
Download
the source code that accompanies this article.
To run the sample
application, you need i5/OS V5R4 installed on your System i and the latest
version of JTOpen or IBM Toolbox for Java JDBC driver. JDK 1.5 or higher is
required to execute the Java code. For maximum database server stability, you
should install the latest database group PTF (SF99504).
The following
publications can be helpful to those who want to learn more about the topics
covered in this article:
Jarek
Miszczyk is the Senior Software Engineer, ISV Solutions Enablement, IBM
Rochester. He can be reached by email at jarek@us.ibm.com. |