Oracle9i JDBC Developer's Guide and Reference Release 1 (9.0.1) Part Number A90211-01 |
|
This chapter discusses the Oracle JDBC implementation of distributed transactions. These are multi-phased transactions, often using multiple databases, that must be committed in a coordinated way. There is also related discussion of XA, which is a general standard (not specific to Java) for distributed transactions.
The following topics are discussed:
For further introductory and general information about distributed transactions, refer to the Sun Microsystems specifications for the JDBC 2.0 Optional Package and the Java Transaction API (JTA).
For information on the OCI-specific HeteroRM XA feature, see "OCI HeteroRM XA".
A distributed transaction, sometimes referred to as a global transaction, is a set of two or more related transactions that must be managed in a coordinated way. The transactions that constitute a distributed transaction might be in the same database, but more typically are in different databases and often in different locations. Each individual transaction of a distributed transaction is referred to as a transaction branch.
For example, a distributed transaction might consist of money being transferred from an account in one bank to an account in another bank. You would not want either transaction committed without assurance that both will complete successfully.
In the JDBC 2.0 extension API, distributed transaction functionality is built on top of connection pooling functionality, described under "Connection Pooling". This distributed transaction functionality is also built upon the open XA standard for distributed transactions. (XA is part of the X/Open standard and is not specific to Java.)
The remainder of this overview covers the following topics:
For further introductory and general information about distributed transactions and XA, refer to the Sun Microsystems specifications for the JDBC 2.0 Optional Package and the Java Transaction API.
In reading the remainder of the distributed transactions section, it will be helpful to keep the following points in mind:
Many vendors will offer XA-compliant JTA modules. This includes Oracle, which is developing a JTA module based on the Oracle implementation of XA discussed below.
In many scenarios, the application server and transaction manager will be together on the middle tier, possibly together with some of the application code as well.
Software that uses distributed transactions cannot use normal connection instance COMMIT
, auto-commit, or ROLLBACK
functionality, because all COMMIT
or ROLLBACK
operations in a distributed transaction must be coordinated. Any attempt to use the commit()
or rollback()
method or enable the auto-commit flag of a connection instance would result in a SQL exception.
When you use XA functionality, the transaction manager uses XA resource instances to prepare and coordinate each transaction branch and then to commit or roll back all transaction branches appropriately.
XA functionality includes the following key components:
There will be one XA data source instance for each resource manager (database) that will be used in the distributed transaction. You will typically create XA data source instances (using the class constructor) in your middle-tier software.
XA data sources produce XA connections.
An XA connection instance corresponds to a single Oracle session, although the session can be used in sequence by multiple logical connection instances (one at a time), as with pooled connection instances.
You will typically get an XA connection instance from an XA data source instance (using a get
method) in your middle-tier software. You can get multiple XA connection instances from a single XA data source instance if the distributed transaction will involve multiple sessions (multiple physical connections) in the same database.
XA connections produce XA resource instances and JDBC connection instances.
You will get one XA resource instance from each XA connection instance (using a get
method), typically in your middle-tier software. There is a one-to-one correlation between XA resource instances and XA connection instances; equivalently, there is a one-to-one correlation between XA resource instances and Oracle sessions (physical connections).
In a typical scenario, the middle-tier component will hand off XA resource instances to the transaction manager, for use in coordinating distributed transactions.
Because each XA resource instance corresponds to a single Oracle session, there can be only a single active transaction branch associated with an XA resource instance at any given time. There can be additional suspended transaction branches, however--see "XA Resource Method Functionality and Input Parameters".
Each XA resource instance has the functionality to start, end, prepare, commit, or roll back the operations of the transaction branch running in the session with which the XA resource instance is associated.
The "prepare" step is the first step of a two-phase COMMIT
operation. The transaction manager will issue a prepare
to each XA resource instance. Once the transaction manager sees that the operations of each transaction branch have prepared successfully (essentially, that the databases can be accessed without error), it will issue a COMMIT
to each XA resource instance to commit all the changes.
Oracle supplies the following three packages that have classes to implement distributed transaction functionality according to the XA standard:
oracle.jdbc.xa
(OracleXid
and OracleXAException
classes)
oracle.jdbc.xa.client
oracle.jdbc.xa.server
Classes for XA data sources, XA connections, and XA resources are in both the client
package and the server
package. (An abstract class for each is in the top-level package.) The OracleXid
and OracleXAException
classes are in the top-level oracle.jdbc.xa
package, because their functionality does not depend on where the code is running.
In middle-tier scenarios, you will import OracleXid
, OracleXAException
, and the oracle.jdbc.xa.client
package.
If you intend your XA code to run in the target Oracle database, however, you will import the oracle.jdbc.xa.server
package instead of the client
package.
If code that will run inside a target database must also access remote databases, then do not import either package--instead, you must fully qualify the names of any classes that you use from the client
package (to access a remote database) or from the server
package (to access the local database). Class names are duplicated between these packages.
This section discusses the XA components--standard XA interfaces specified in the JDBC 2.0 Optional Package, and the Oracle classes that implement them. The following topics are covered:
The javax.sql.XADataSource
interface outlines standard functionality of XA data sources, which are factories for XA connections. The overloaded getXAConnection()
method returns an XA connection instance and optionally takes a user name and password as input:
public interface XADataSource { XAConnection getXAConnection() throws SQLException; XAConnection getXAConnection(String user, String password) throws SQLException; ... }
Oracle JDBC implements the XADataSource
interface with the OracleXADataSource
class, located both in the oracle.jdbc.xa.client package
and the oracle.jdbc.xa.server
package.
The OracleXADataSource
classes also extend the OracleConnectionPoolDataSource
class (which extends the OracleDataSource
class), so include all the connection properties described in "Data Source Properties".
The OracleXADataSource
class getXAConnection()
methods return the Oracle implementation of XA connection instances, which are OracleXAConnection
instances (as the next section discusses).
Note: You can register XA data sources in JNDI using the same naming conventions as discussed previously for non-pooling data sources in "Register the Data Source". |
An XA connection instance, as with a pooled connection instance, encapsulates a physical connection to a database. This would be the database specified in the connection properties of the XA data source instance that produced the XA connection instance.
Each XA connection instance also has the facility to produce the XA resource instance that will correspond to it for use in coordinating the distributed transaction.
An XA connection instance is an instance of a class that implements the standard javax.sql.XAConnection
interface:
public interface XAConnection extends PooledConnection { javax.jta.xa.XAResource getXAResource() throws SQLException; }
As you see, the XAConnection
interface extends the javax.sql.PooledConnection
interface, so it also includes the getConnection()
, close()
, addConnectionEventListener()
, and removeConnectionEventListener()
methods listed in "Pooled Connection Interface and Oracle Implementation".
Oracle JDBC implements the XAConnection
interface with the OracleXAConnection
class, located both in the oracle.jdbc.xa.client package
and the oracle.jdbc.xa.server
package.
The OracleXAConnection
classes also extend the OraclePooledConnection
class.
The OracleXAConnection
class getXAResource()
method returns the Oracle implementation of an XA resource instance, which is an OracleXAResource
instance (as the next section discusses). The getConnection()
method returns an OracleConnection
instance.
A JDBC connection instance returned by an XA connection instance acts as a temporary handle to the physical connection, as opposed to encapsulating the physical connection. The physical connection is encapsulated by the XA connection instance.
Each time an XA connection instance getConnection()
method is called, it returns a new connection instance that exhibits the default behavior, and closes any previous connection instance that still exists and had been returned by the same XA connection instance. It is advisable to explicitly close any previous connection instance before opening a new one, however.
Calling the close()
method of an XA connection instance closes the physical connection to the database. This is typically performed in the middle tier.
The transaction manager uses XA resource instances to coordinate all the transaction branches that constitute a distributed transaction.
Each XA resource instance provides the following key functionality, typically invoked by the transaction manager:
COMMIT
functionality of a distributed transaction to ensure that changes are not committed in one transaction branch before there is assurance that the changes will succeed in all transaction branches.
"XA Resource Method Functionality and Input Parameters" further discusses this.
An XA resource instance is an instance of a class that implements the standard javax.transaction.xa.XAResource
interface:
public interface XAResource { void commit(Xid xid, boolean onePhase) throws XAException; void end(Xid xid, int flags) throws XAException; void forget(Xid xid) throws XAException; int prepare(Xid xid) throws XAException; Xid[] recover(int flag) throws XAException; void rollback(Xid xid) throws XAException; void start(Xid xid, int flags) throws XAException; boolean isSameRM(XAResource xares) throws XAException; }
Oracle JDBC implements the XAResource
interface with the OracleXAResource
class, located both in the oracle.jdbc.xa.client package
and the oracle.jdbc.xa.server
package.
The Oracle JDBC driver creates and returns an OracleXAResource
instance whenever the OracleXAConnection
class getXAResource()
method is called, and it is the Oracle JDBC driver that associates an XA resource instance with a connection instance and the transaction branch being executed through that connection.
This method is how an OracleXAResource
instance is associated with a particular connection and with the transaction branch being executed in that connection.
The OracleXAResource
class has several methods to coordinate a transaction branch with the distributed transaction with which it is associated. This functionality usually involves two-phase COMMIT
operations.
A transaction manager, receiving OracleXAResource
instances from a middle-tier component such as an application server, typically invokes this functionality.
Each of these methods takes a transaction ID as input, in the form of an Xid
instance, which includes a transaction branch ID component and a distributed transaction ID component. Every transaction branch has a unique transaction ID, but transaction branches belonging to the same global transaction have the same global transaction component as part of their transaction IDs.
"XA ID Interface and Oracle Implementation" discusses the OracleXid
class and the standard interface upon which it is based.
Following is a description of key XA resource functionality, the methods used, and additional input parameters. Each of these methods throws an XA exception if an error is encountered. See "XA Exception Classes and Methods".
Start work on behalf of a transaction branch, associating the transaction branch with a distributed transaction.
void start(Xid xid, int flags)
The flags
parameter can have one of the following values:
XAResource.TMNOFLAGS
(no special flag)--This is to flag the start of a new transaction branch for subsequent operations in the session associated with this XA resource instance. This branch will have the transaction ID xid
, which is an OracleXid
instance created by the transaction manager. This will map the transaction branch to the appropriate distributed transaction.
XAResource.TMJOIN
--This is to join subsequent operations in the session associated with this XA resource instance to the existing transaction branch specified by xid
.
XAResource.TMRESUME
--This is to resume the transaction branch specified by xid
. (It must first have been suspended.)
TMNOFLAGS
, TMJOIN
, and TMRESUME
are defined as static members of the XAResource
interface and OracleXAResource
class.
Note that to create an appropriate transaction ID in starting a transaction branch, the transaction manager must know which distributed transaction the transaction branch should belong to. The mechanics of this are handled between the middle tier and transaction manager and are beyond the scope of this document. Refer to the Sun Microsystems specifications for the JDBC 2.0 Optional Package and the Java Transaction API.
End work on behalf of the transaction branch specified by xid
, disassociating the transaction branch from its distributed transaction.
void end(Xid xid, int flags)
The flags
parameter can have one of the following values:
XAResource.TMSUCCESS
--This is to indicate that this transaction branch is known to have succeeded.
XAResource.TMFAIL
--This is to indicate that this transaction branch is known to have failed.
XAResource.TMSUSPEND
--This is to suspend the transaction branch specified by xid
. (By suspending transaction branches, you can have multiple transaction branches in a single session. Only one can be active at any given time, however. Also, this tends to be more expensive in terms of resources than having two sessions.)
TMSUCCESS
, TMFAIL
, and TMSUSPEND
are defined as static members of the XAResource
interface and OracleXAResource
class.
Prepare the changes performed in the transaction branch specified by xid
. This is the first phase of a two-phase COMMIT
operation, to ensure that the database is accessible and that the changes can be committed successfully.
int prepare(Xid xid)
This method returns an integer value as follows:
XAResource.XA_RDONLY
--This is returned if the transaction branch executes only read-only operations such as SELECT
statements.
XAResource.XA_OK
--This is returned if the transaction branch executes updates that are all prepared without error.
XA_RDONLY
and XA_OK
are defined as static members of the XAResource
interface and OracleXAResource
class.
Commit prepared changes in the transaction branch specified by xid
. This is the second phase of a two-phase COMMIT
and is performed only after all transaction branches have been successfully prepared.
void commit(Xid xid, boolean onePhase)
Set the onePhase
parameter as follows:
true
--This is to use one-phase instead of two-phase protocol in committing the transaction branch. This is appropriate if there is only one transaction branch in the distributed transaction; the prepare
step would be skipped.
false
--This is to use two-phase protocol in committing the transaction branch (typical).
Rolls back prepared changes in the transaction branch specified by xid
.
void rollback(Xid xid)
Tells the resource manager to forget about a heuristically completed transaction branch.
public void forget(Xid xid)
The transaction manager calls this method during recovery to obtain the list of transaction branches that are currently in prepared or heuristically completed states.
public Xid[] recover(int flag)
The resource manager returns zero or more Xid
s for the transaction branches that are currently in a prepared or heuristically completed state. If an error occurs during the operation, the resource manager throws the appropriate XAException
.
To determine if two XA resource instances correspond to the same resource manager (database), call the isSameRM()
method from one XA resource instance, specifying the other XA resource instance as input. In the following example, presume xares1
and xares2
are OracleXAResource
instances:
boolean sameRM = xares1.isSameRM(xares2);
A transaction manager can use this method regarding certain Oracle optimizations, as "Oracle XA Optimizations" explains.
The transaction manager creates transaction ID instances and uses them in coordinating the branches of a distributed transaction. Each transaction branch is assigned a unique transaction ID, which includes the following information:
A format identifier specifies a Java transaction manager--for example, there could be a format identifier ORCL
. This field cannot be null.
The 64-byte global transaction identifier value will be identical in the transaction IDs of all transaction branches belonging to the same distributed transaction. The overall transaction ID, however, is unique for every transaction branch.
An XA transaction ID instance is an instance of a class that implements the standard javax.transaction.xa.Xid
interface, which is a Java mapping of the X/Open transaction identifier XID structure.
Oracle implements this interface with the OracleXid
class in the oracle.jdbc.xa
package. OracleXid
instances are employed only in a transaction manager, transparent to application programs or an application server.
A transaction manager may use the following in creating an OracleXid
instance:
public OracleXid(int fId, byte gId[], byte bId[]) throws XAException
Where fId
is an integer value for the format identifier, gId[]
is a byte array for the global transaction identifier, and bId[]
is a byte array for the branch qualifier.
The Xid
interface specifies the following getter methods:
This section has two focuses: 1) the functionality of XA exceptions and error handling; and 2) Oracle optimizations in its XA implementation. The following topics are covered:
The exception and error-handling discussion includes the standard XA exception class and the Oracle-specific XA exception class, as well as particular XA error codes and error-handling techniques.
XA methods throw XA exceptions, as opposed to general exceptions or SQL exceptions. An XA exception is an instance of the standard class javax.transaction.xa.XAException
or a subclass. Oracle subclasses XAException
with the oracle.jdbc.xa.OracleXAException
class.
An OracleXAException
instance consists of an Oracle error portion and an XA error portion and is constructed as follows by the Oracle JDBC driver:
public OracleXAException()
or:
public OracleXAException(int error)
The error value is an error code that combines an Oracle SQL error value and an XA error value. (The JDBC driver determines exactly how to combine the Oracle and XA error values.)
The OracleXAException
class has the following methods:
public int getOracleError()
This method returns the Oracle SQL error code pertaining to the exception--a standard ORA error number (or 0 if there is no Oracle SQL error).
public int getXAError()
This method returns the XA error code pertaining to the exception. XA error values are defined in the javax.transaction.xa.XAException
class; refer to its Javadoc at the Sun Microsystems Web site for more information.
Oracle errors correspond to XA errors in OracleXAException
instances as documented in Table 15-1.
The following example uses the OracleXAException
class to process an XA exception:
try { ... ...Perform XA operations... ... } catch(OracleXAException oxae) { int oraerr = oxae.getOracleError(); System.out.println("Error " + oraerr); } catch(XAException xae) {...Process generic XA exception...}
In case the XA operations did not throw an Oracle-specific XA exception, the code drops through to process a generic XA exception.
Oracle JDBC has functionality to improve performance if two or more branches of a distributed transaction use the same database instance--meaning that the XA resource instances associated with these branches are associated with the same resource manager.
In such a circumstance, the prepare()
method of only one of these XA resource instances will return XA_OK
(or failure); the rest will return XA_RDONLY
, even if updates are made. This allows the transaction manager to implicitly join all the transaction branches and commit (or roll back, if failure) the joined transaction through the XA resource instance that returned XA_OK
(or failure).
The transaction manager can use the OracleXAResource
class isSameRM()
method to determine if two XA resource instances are using the same resource manager. This way it can interpret the meaning of XA_RDONLY
return values.
This section provides an example of how to implement a distributed transaction using Oracle XA functionality.
You must import the following for Oracle XA functionality:
import oracle.jdbc.xa.OracleXid; import oracle.jdbc.xa.OracleXAException; import oracle.jdbc.pool.*; import oracle.jdbc.xa.client.*; import javax.transaction.xa.*;
The oracle.jdbc.pool
package has classes for connection pooling functionality, some of which are subclassed by XA-related classes.
In addition, if the code will run inside an Oracle database and access that database for SQL operations, you must import the following:
import oracle.jdbc.xa.server.*;
(And if you intend to access only the database in which the code runs, you would not need the oracle.jdbc.xa.client
classes.)
The client
and server
packages each have versions of the OracleXADataSource
, OracleXAConnection
, and OracleXAResource
classes. Abstract versions of these three classes are in the top-level oracle.jdbc.xa
package.
This example uses a two-phase distributed transaction with two transaction branches, each to a separate database.
Note that for simplicity, this example combines code that would typically be in a middle tier with code that would typically be in a transaction manager (such as the XA resource method invocations and the creation of transaction IDs).
For brevity, the specifics of creating transaction IDs (in the createID()
method) and performing SQL operations (in the doSomeWork1()
and doSomeWork2()
methods) are not shown here. The complete sample is in "XA with Two-Phase Commit Operation--XA4.java".
For another complete sample, showing how to use XA resource functionality to suspend and resume a transaction, see "XA with Suspend and Resume--XA2.java".
This example executes the following sequence:
// You need to import the java.sql package to use JDBC import java.sql.*; import javax.sql.*; import oracle.jdbc.*; import oracle.jdbc.pool.*; import oracle.jdbc.xa.OracleXid; import oracle.jdbc.xa.OracleXAException; import oracle.jdbc.xa.client.*; import javax.transaction.xa.*; class XA4 { public static void main (String args []) throws SQLException { try { String URL1 = "jdbc:oracle:oci8:@"; String URL2 ="jdbc:oracle:thin:@(description=(address=(host=dlsun991) (protocol=tcp)(port=5521))(connect_data=(sid=rdbms2)))"; DriverManager.registerDriver(new OracleDriver()); // You can put a database name after the @ sign in the connection URL. Connection conna = DriverManager.getConnection (URL1, "scott", "tiger"); // Prepare a statement to create the table Statement stmta = conna.createStatement (); Connection connb = DriverManager.getConnection (URL2, "scott", "tiger"); // Prepare a statement to create the table Statement stmtb = connb.createStatement (); try { // Drop the test table stmta.execute ("drop table my_table"); } catch (SQLException e) { // Ignore an error here } try { // Create a test table stmta.execute ("create table my_table (col1 int)"); } catch (SQLException e) { // Ignore an error here too } try { // Drop the test table stmtb.execute ("drop table my_tab"); } catch (SQLException e) { // Ignore an error here } try { // Create a test table stmtb.execute ("create table my_tab (col1 char(30))"); } catch (SQLException e) { // Ignore an error here too } // Create XADataSource instances and set properties. OracleXADataSource oxds1 = new OracleXADataSource(); oxds1.setURL("jdbc:oracle:oci8:@"); oxds1.setUser("scott"); oxds1.setPassword("tiger"); OracleXADataSource oxds2 = new OracleXADataSource(); oxds2.setURL("jdbc:oracle:thin:@(description=(address=(host=dlsun991) (protocol=tcp)(port=5521))(connect_data=(sid=rdbms2)))"); oxds2.setUser("scott"); oxds2.setPassword("tiger"); // Get XA connections to the underlying data sources XAConnection pc1 = oxds1.getXAConnection(); XAConnection pc2 = oxds2.getXAConnection(); // Get the physical connections Connection conn1 = pc1.getConnection(); Connection conn2 = pc2.getConnection(); // Get the XA resources XAResource oxar1 = pc1.getXAResource(); XAResource oxar2 = pc2.getXAResource(); // Create the Xids With the Same Global Ids Xid xid1 = createXid(1); Xid xid2 = createXid(2); // Start the Resources oxar1.start (xid1, XAResource.TMNOFLAGS); oxar2.start (xid2, XAResource.TMNOFLAGS); // Execute SQL operations with conn1 and conn2 doSomeWork1 (conn1); doSomeWork2 (conn2); // END both the branches -- IMPORTANT oxar1.end(xid1, XAResource.TMSUCCESS); oxar2.end(xid2, XAResource.TMSUCCESS); // Prepare the RMs int prp1 = oxar1.prepare (xid1); int prp2 = oxar2.prepare (xid2); System.out.println("Return value of prepare 1 is " + prp1); System.out.println("Return value of prepare 2 is " + prp2); boolean do_commit = true; if (!((prp1 == XAResource.XA_OK) || (prp1 == XAResource.XA_RDONLY))) do_commit = false; if (!((prp2 == XAResource.XA_OK) || (prp2 == XAResource.XA_RDONLY))) do_commit = false; System.out.println("do_commit is " + do_commit); System.out.println("Is oxar1 same as oxar2 ? " + oxar1.isSameRM(oxar2)); if (prp1 == XAResource.XA_OK) if (do_commit) oxar1.commit (xid1, false); else oxar1.rollback (xid1); if (prp2 == XAResource.XA_OK) if (do_commit) oxar2.commit (xid2, false); else oxar2.rollback (xid2); // Close connections conn1.close(); conn1 = null; conn2.close(); conn2 = null; pc1.close(); pc1 = null; pc2.close(); pc2 = null; ResultSet rset = stmta.executeQuery ("select col1 from my_table"); while (rset.next()) System.out.println("Col1 is " + rset.getInt(1)); rset.close(); rset = null; rset = stmtb.executeQuery ("select col1 from my_tab"); while (rset.next()) System.out.println("Col1 is " + rset.getString(1)); rset.close(); rset = null; stmta.close(); stmta = null; stmtb.close(); stmtb = null; conna.close(); conna = null; connb.close(); connb = null; } catch (SQLException sqe) { sqe.printStackTrace(); } catch (XAException xae) { if (xae instanceof OracleXAException) { System.out.println("XA Error is " + ((OracleXAException)xae).getXAError()); System.out.println("SQL Error is " + ((OracleXAException)xae).getOracleError()); } } } static Xid createXid(int bids) throws XAException {...Create transaction IDs...} private static void doSomeWork1 (Connection conn) throws SQLException {...Execute SQL operations...} private static void doSomeWork2 (Connection conn) throws SQLException {...Execute SQL operations...} }
|
Copyright © 1996-2001, Oracle Corporation. All Rights Reserved. |
|