Oracle XA example

Source: Internet
Author: User
Tags mysql host sql error

/*
A simple 2 phase XA demo. Both the branches talk to different rms
Need 2 Java enabled 8.1.6 databases to run this demo.
-> Start-1
-> Start-2
-> Do some DML on 1
-> Do some DML on 2
-> End 1
-> End 2
-> Prepare-1
-> Prepare-2
-> Commit-1
-> Commit-2
Please supply hostname as ARGs [0],
Port Number as ARGs [1],
And SERVICE_NAME as ARGs [2]
To make the url2 when run this.
As: Java Xa4 MySQL host 5521 orcl.us.oracle.com

Please use jdk1.2 or later version
*/

// 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
{
If (ARGs. length! = 3)
{
System. Out. println ("Usage: Java Xa4 System. Exit (0 );
}

String host = ARGs [0];
String Port = ARGs [1];
String SERVICE_NAME = ARGs [2];

Try
{
String url1 = "JDBC: oracle: oci8 :@";
String url2 = "JDBC: oracle: thin: @ (description = (address = (host =" + host
+ ") (Protocol = TCP) (Port =" + port + ") (CONNECT_DATA = (SERVICE_NAME ="
+ SERVICE_NAME + ")))";

Try {
String url1 = system. getproperty ("jdbc_url ");
If (url1! = NULL)
Url1 = url1;
} Catch (exception e ){
// If there is any security exception, ignore it
// And use the default
}

Try {
String url2 = system. getproperty ("jdbc_url_2 ");
If (url2! = NULL)
Url2 = url2;
} Catch (exception e ){
// If there is any security exception, ignore it
// And use the default
}

// Create a oracledatasource instance and Set Properties
Oracledatasource odsa = new oracledatasource ();
Odsa. setuser ("HR ");
Odsa. setpassword ("HR ");
Odsa. seturl (url1 );

Connection Conna = odsa. getconnection ();

// Prepare a statement to create the table
Statement statement TA = Conna. createstatement ();

// Create another oracledatasource
Oracledatasource odsb = new oracledatasource ();
Odsb. setuser ("HR ");
Odsb. setpassword ("HR ");
Odsb. seturl (url2 );
Connection connb = odsb. getconnection ();

Statement stmtb = connb. createstatement ();

// Close odsa and odsb won't affect
// Previously generated connections and statements
Odsa. Close ();
Odsb. Close ();

Try
{
Ta.exe cute ("delete from jobs where job_id = 'SC _ stuff '");
}
Catch (sqlexception E)
{
// Ignore an error here
}

Try
{
Stmtb.exe cute ("delete from regions where region_id> 100 ");
}
Catch (sqlexception E)
{
// Ignore an error here
}

// Create a xadatasource instance
Oraclexadatasource oxds1 = new oraclexadatasource ();
Oxds1.seturl (url1 );
Oxds1.setuser ("HR ");
Oxds1.setpassword ("HR ");

Oraclexadatasource oxds2 = new oraclexadatasource ();
Oxds2.seturl (url2 );
Oxds2.setuser ("HR ");
Oxds2.setpassword ("HR ");

// Get a XA connection to the underlying data source
Xaconnection pC1 = oxds1.getxaconnection ();

// We can use the same data source
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 );

// Do something with conn1 and conn2
Dosomework1 (conn1 );
Dosomework2 (conn2 );

// End both the branches -- this is must
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 = paita.exe cutequery ("select job_id, job_title from jobs ");
System. Out. println ("/ncontents of table jobs:/N ");
While (rset. Next ())
System. Out. println (rset. getstring (1) + "" + rset. getstring (2 ));

Rset. Close ();
Rset = NULL;

Rset = stmtb.exe cutequery ("select region_id, region_name from regions order by region_id ");
System. Out. println ("/ncontents of table regions:/N ");
While (rset. Next ())
System. Out. println (rset. getint (1) + "" + rset. getstring (2 ));

Rset. Close ();
Rset = NULL;

Stmta. Close ();
Required TA = 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
{
Byte [] gid = new byte [1]; GID [0] = (byte) 9;
Byte [] bid = new byte [1]; bid [0] = (byte) bids;
Byte [] gtrid = new byte [64];
Byte [] bqual = new byte [64];
System. arraycopy (GID, 0, gtrid, 0, 1 );
System. arraycopy (BID, 0, bqual, 0, 1 );
Xid = new oraclexid (0x1234, gtrid, bqual );
Return Xid;
}

Private Static void dosomework1 (connection conn)
Throws sqlexception
{
// Create a statement
Statement stmt = conn. createstatement ();

Int CNT = stmt.exe cuteupdate ("insert into jobs values ('SC _ stuff', 'security stuff ', null, null )");

System. Out. println ("No of rows affected" + CNT );

Stmt. Close ();
Stmt = NULL;
}

Private Static void dosomework2 (connection conn)
Throws sqlexception
{
// Create a statement
Statement stmt = conn. createstatement ();

Int CNT = stmt.exe cuteupdate ("insert into regions values (101, 'AFRICA ')");

System. Out. println ("No of rows affected" + CNT );

Stmt. Close ();
Stmt = NULL;
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.