IBM DB2 Manual page 490

Table of Contents

Advertisement

javax.transaction.UserTransaction utx;
// Use the begin method on a UserTransaction object to indicate
// the beginning of a global transaction.
utx.begin();
...
// Execute some SQL with one Connection object.
// Do not call Connection methods commit or rollback.
...
// Use the commit method on the UserTransaction object to
// drive all transaction branches to commit and indicate
// the end of the global transaction.
utx.commit();
...
Figure 60. Example of a global transaction
When you run a multi-threaded client under WebSphere, a transaction can span
multiple threads. This situation might occur in a Java servlet. An application that
runs in this environment needs to perform some SQL on each Connection object
before the application passes the object to another thread. The following code
illustrates this point.
javax.transaction.UserTransaction utx;
// Use the begin method on a UserTransaction object to indicate
// the beginning of a global transaction.
utx.begin();
...
// Obtain two JDBC Connections from DataSource ds
c1 = ds.getConnection();
c2 = ds.getConnection();
...
// Create a thread for each Connection object
ThreadClass1 tc1 = new ThreadClass1(c1);
ThreadClass2 tc2 = new ThreadClass1(c2);
Thread t1 = new Thread(tc1);
Thread t2 = new Thread(tc2);
// Execute some SQL on each Connection object to associate
// the threads with the global transaction
...
// Start the two threads that will use the Connection objects to do SQL
t1.start();
t2.start();
...
// Use the commit method on the UserTransaction object to
// drive all transaction branches to commit and indicate
// the end of the global transaction.
utx.commit();
...
Figure 61. Example of a global transaction in a multi-threaded environment
474
Application Programming Guide and Reference for Java
Related concepts
Chapter 12, "IBM Data Server Driver for JDBC and SQLJ type 4 connectivity
JDBC and SQLJ distributed transaction support," on page 467

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents