Hello everybody.
I did the following agenda based program that found in Iternet on JTA:
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
//JTA APIs
import javax.sql.XAConnection;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
// Oracle
JDBC Transaction APIs
import oracle.jdbc.xa.OracleXid;
import oracle.jdbc.xa.client.OracleXADataSource;
public class JTAEjemplo {
/**
* This method returns a Transaction ID for the Transaction. The transaction
* ID contains 2 parts, a global Transaction ID set to 9 in this method and
* a Branch ID that is unique to each branch set to the value passed as
* parameter.
*/
private static Xid createXid( int pbID ) throws XAException {
byte[] gID = new byte[ 1 ]; // Global ID
gID[ 0 ] = (byte) 9; // Set Global Id to 9
byte[] bID = new byte[ 1 ]; // Branch ID
bID[ 0 ] = (byte) pbID; // Set branch ID to the parameter passed.
byte[] globalID = new byte[ 64 ];
byte[] branchID = new byte[ 64 ];
// Copy the Global ID and branch ID to a 64 bit
string.
System.arraycopy( gID, 0, globalID, 0, 1 );
System.arraycopy( bID, 0, branchID, 0, 1 );
// Call OracleXid() to generate the Xid.
Xid xid = new OracleXid( 0x1234, globalID, branchID );
// Return the Transaction ID.
return xid;
}
public void prueba() {
OracleXADataSource oxds1 = null;
OracleXADataSource oxds2 = null;
//XAConnection pc1 = null;
//XAConnection pc2 = null;
Connection conn1 = null;
Connection conn2 = null;
XAResource oxar1 = null;
XAResource oxar2 = null;
Statement stmt1 = null;
Statement stmt2 = null;
Xid xid1 = null;
Xid xid2 = null;
try {
System.out.println("Conexion 1");
System.out.println("Entro prueba");
oxds1 = new OracleXADataSource();
oxds1.setURL("jdbc
racle:thin:@//lotenal2c:1541/SILT");
oxds1.setUser("sild");
oxds1.setPassword("admin");
System.out.println("Conexion 1");
oxds2 = new OracleXADataSource();
oxds2.setURL("jdbc
racle:thin:@//lotenal2c:1541/SILT");
oxds2.setUser("sild");
oxds2.setPassword("admin");
// Get XA connections to the underlying datasources
//pc1 = oxds1.getXAConnection();
//pc2 = oxds2.getXAConnection();
// Get the XA resources
oxar1 = oxds1.getXAConnection().getXAResource();
oxar2 = oxds1.getXAConnection().getXAResource();
// Get the physical connections
conn1 = oxds1.getXAConnection().getConnection();
conn2 = oxds1.getXAConnection().getConnection();
System.out.println("CRea statement");
// Create Statements for updating.
stmt1 = conn1.createStatement();
stmt2 = conn2.createStatement();
// Create the Xids With the Same Global Ids
xid1 = createXid(1);
//xid2 = createXid(2);
System.out.println("Ejecuta operacion 1");
// Start the Resources
oxar1.start (xid1, XAResource.TMNOFLAGS);
stmt1.executeUpdate("INSERT INTO oficinas VALUES(2,'IGUALA GRO')");
// Suspend the transactions.
oxar1.end( xid1, XAResource.TMSUCCESS);
System.out.println("Ejecuta operacion 2");
// Start the Resources
oxar2.start (xid1, XAResource.TMJOIN);
stmt2.executeUpdate("INSERT INTO productos VALUES(X,'Chocolate')");
// Suspend the transactions.
oxar2.end( xid1, XAResource.TMSUCCESS);
// Prepare the Resource Managers
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);
if (prp1 == XAResource.XA_OK)
if (do_commit)
oxar1.commit(xid1, false);
else
oxar1.rollback(xid1);
//if (prp2 == XAResource.XA_OK)
//if (do_commit)
//oxar1.commit(xid2, false);
//else
//oxar1.rollback(xid2);
} catch(Exception ex) {
System.out.println("Distributed transaction prepare/commit failed. Rolling it back.");
ex.printStackTrace();
//try {
//oxar1.rollback(xid1);
//} catch (javax.transaction.xa.XAException xae1) { // Report failure of rollback.
//System.out.println("distributed Transaction rollback oxar1 failed id1");
//System.out.println("XAException error code = " + xae1.errorCode);
//System.out.println("XAException message = " + xae1.getMessage());
//}
//try {
//oxar2.rollback(xid1);
//} catch (javax.transaction.xa.XAException xae2) { // Report failure of rollback.
//System.out.println("distributed Transaction rollback oxar1 failed xid2");
//System.out.println("XAException error code = " + xae2.errorCode);
//System.out.println("XAException message = " + xae2.getMessage());
//}
}
finally {
if ( stmt1 != null ) {
try {
stmt1.close( );
} catch ( SQLException e ) { // Catch SQL Errors
e.printStackTrace();
}
}
if ( stmt2 != null ) {
try {
stmt2.close( );
} catch ( SQLException e ) { // Catch SQL Errors
e.printStackTrace();
}
}
if ( conn1 != null ) {
try {
conn1.close( );
} catch ( SQLException e ) { // Catch SQL Errors
e.printStackTrace();
}
}
if ( conn2 != null ) {
try {
conn2.close( );
} catch ( SQLException e ) { // Catch SQL Errors
e.printStackTrace();
}
}
}
}
}
The program is run, but it's not doing what should be done. The program ran 2 inserts, but the second is wrong. When I run the program should not sign any of the inserts in the database, however, if the first one runs it with success, when according to the program should not run any of the 2. What is that this attempt to run as a single transaction and not as two separate transactions
Someone can help me, it changed in several ways, but I get none.
Annex the definitions of the tables
oficina integer not null PRIMARY KEY,
ciudad char(15) not null
producto integer not null PRIMARY KEY,
descripcion char(15)
Excuseme for my bad English