In the past few days, we have been working on how to implement distributed transactions. I found a lot of materials, but most of them are similar. The number of Oracle, SQL Server, and MySQL has been tested. Only MySQL or above supports distributed transactions.
I have never been involved in distributed transactions before. I also found some information about Distributed Transaction databases by mistake. The result is not what I need now.
Many errors occurred during the test, but they never passed. I thought it was a problem with user permissions and database services. However, when everything was well configured, it still failed. The results showed that I imported some common JDBC connection packages, so I searched for jar packages to implement XA transactions.
MySQL: mysql-connector-java-5.1.6-bin.jar
SQL Server: sqljdbc. Jar
ORACLE: ojdbc14.jar
These packages run smoothly. These jar packages will be attached later.
Okay. Add the source code as well:
Import com. Microsoft. sqlserver. JDBC. sqlserverxadatasource;
Import com. MySQL. JDBC. jdbc2.optional. mysqlxadatasource;
Import java. SQL .*;
Import java. util. Logging. level;
Import java. util. Logging. Logger;
Import javax. SQL .*;
Import javax. transaction. Xa .*;
Import oracle. JDBC. Xa. Client. oraclexadatasource;
Public class mutil_performance_test {
Public static void main (string [] ARGs ){
Mutil_performance_test MDT = new mutil_performance_test ();
Try {
MDT. test1 ();
} Catch (exception ex ){
System. Out. println ("exceptions except sqlexception and xaexception: \ n ");
Logger. getlogger (mutil_performance_test.class.getname (). Log (level. Severe, null, ex );
}
}
Class myxid implements Xid {
Int formatid;
Byte globaltransactionid [];
Byte branchqualifier [];
Public myxid (){
}
Public myxid (INT formatid, byte [] globaltransactionid, byte [] branchqualifier ){
This. formatid = formatid;
This. globaltransactionid = globaltransactionid;
This. branchqualifier = branchqualifier;
}
Public int getformatid (){
Return this. formatid;
}
Public void setformatid (INT formatid ){
This. formatid = formatid;
}
Public byte [] getglobaltransactionid (){
Return this. globaltransactionid;
}
Public void setglobaltransactionid (byte [] globaltransactionid ){
This. globaltransactionid = globaltransactionid;
}
Public byte [] getbranchqualifier (){
Return this. branchqualifier;
}
Public void setbranchqualifier (byte [] branchqualifier ){
This. branchqualifier = branchqualifier;
}
}
// Multi-database Test
Public void test1 (){
// Define the required variables
Connection mysqlcn = NULL;
Connection sqlcn = NULL;
Connection mysqlcn2 = NULL;
Connection limit n = NULL;
Mysqlxadatasource mysqlds = NULL;
Sqlserverxadatasource sqlds = NULL;
Mysqlxadatasource mysqlds2 = NULL;
Oraclexadatasource orads = NULL;
Xaconnection xamysqlcn = NULL;
Xaconnection xasqlcn = NULL;
Xaconnection xamysqlcn2 = NULL;
Xaconnection xaoracn = NULL;
Xaresource xamysqlres = NULL;
Xaresource xasqlres = NULL;
Xaresource xamysqlres2 = NULL;
Xaresource xaorares = NULL;
Xid mysqlxid = NULL;
Xid sqlxid = NULL;
Xid mysqlxid2 = NULL;
Xid oraxid = NULL;
Statement mysqlpst = NULL;
Statement sqlpst = NULL;
Statement mysqlpst2 = NULL;
Statement orapst = NULL;
Try {
// Obtain the data source
Mysqlds = new mysqlxadatasource ();
Mysqlds. seturl ("JDBC: mysql: // localhost: 3306/test ");
Mysqlds2 = new mysqlxadatasource ();
Mysqlds2.seturl ("JDBC: mysql: // 10.10.119: 3306/test ");
Sqlds = new sqlserverxadatasource ();
Sqlds. seturl ("JDBC: sqlserver: // 10.10.119: 1433; databasename = RTC; logintimeout = 20; user = sa; Password = chgpwd122105 ");
// Sqlds. setuser ("sa ");
// Sqlds. setpassword ("chgpwd122105 ");
// Sqlds. setservername ("10.10.10.119 ");
// Sqlds. setportnumber (1433 );
// Sqlds. setdatabasename ("RTC ");
Orads = new oraclexadatasource ();
Orads. seturl ("JDBC: oracle: thin: @ 10.10.119: 1521: WMS ");
// Obtain the connection
Xamysqlcn = mysqlds. getxaconnection ("root", "9999 ");
System. Out. println ("xamysqlcn:" + xamysqlcn );
Xasqlcn = sqlds. getxaconnection ();
System. Out. println ("xasqlcn:" + xasqlcn );
Xamysqlcn2 = mysqlds2.getxaconnection ("root", "9999 ");
System. Out. println ("xamysqlcn2:" + xamysqlcn2 );
Xaoracn = orads. getxaconnection ("tiger", "Tiger ");
System. Out. println ("xa0000n:" + xa0000n );
Mysqlcn = xamysqlcn. getconnection ();
Sqlcn = xasqlcn. getconnection ();
Mysqlcn2 = xamysqlcn2.getconnection ();
Limit n = xaoracn. getconnection ();
Mysqlpst = mysqlcn. createstatement ();
Sqlpst = sqlcn. createstatement ();
Mysqlpst2 = mysqlcn2.createstatement ();
Orapst = snapshot N. createstatement ();
// Define xaresource
Xamysqlres = xamysqlcn. getxaresource ();
Xasqlres = xasqlcn. getxaresource ();
Xamysqlres2 = xamysqlcn2.getxaresource ();
Xaorares = xaoracn. getxaresource ();
// Define the XId
Mysqlxid = new myxid (0, new byte [] {0x01}, new byte [] {0x02 });
Sqlxid = new myxid (0, new byte [] {0x01}, new byte [] {0x03 });
Mysqlxid2 = new myxid (0, new byte [] {0x01}, new byte [] {0x04 });
Oraxid = new myxid (0, new byte [] {0x01}, new byte [] {0x05 });
// Execute MySQL
Xamysqlres. Start (mysqlxid, xaresource. tmnoflags );
Mysqlpst.exe cuteupdate ("insert into test values (4, 'xa ', 'F', 'class4 ')");
Xamysqlres. End (mysqlxid, xaresource. tmsuccess );
// Execute sqlserver
Xasqlres. Start (sqlxid, xaresource. tmnoflags );
Sqlpst.exe cuteupdate ("insert into test values ('20140901 ')");
Xasqlres. End (sqlxid, xaresource. tmsuccess );
// Execute MySQL
Xamysqlres2.start (mysqlxid2, xaresource. tmnoflags );
Mysqlpst2.executeupdate ("insert into test values (4, 'xa ', 'F', 'class4 ')");
Xamysqlres2.end (mysqlxid2, xaresource. tmsuccess );
// Execute Oracle
System. Out. println ("xaorares:" + xaorares );
Xaorares. Start (oraxid, xaresource. tmnoflags );
Orapst.exe cuteupdate ("insert into test123 values ('4', '44', '123 ')");
System. Out. println ("oraxid:" + oraxid );
Xaorares. End (oraxid, xaresource. tmsuccess );
// Prepare
Int mysqlrea = xamysqlres. Prepare (mysqlxid );
Int sqlrea = xasqlres. Prepare (sqlxid );
Int mysqlrea2 = xamysqlres2.prepare (mysqlxid2 );
Int orarea = xaorares. Prepare (oraxid );
// Determine whether the database is ready for submission or rollback
If (mysqlrea = xamysqlres. xa_ OK & mysqlrea2 = xamysqlres. xa_ OK & orarea = xaorares. xa_ OK & sqlrea = xasqlres. xa_ OK ){
// If (mysqlrea = xamysqlres. xa_ OK & mysqlrea2 = xamysqlres. xa_ OK & orarea = xaorares. xa_ OK ){
// If (mysqlrea = xamysqlres. xa_ OK & sqlrea = xasqlres. xa_ OK & mysqlrea2 = xamysqlres. xa_ OK ){
// If (mysqlrea = xamysqlres. xa_ OK & mysqlrea2 = xamysqlres. xa_ OK ){
Xamysqlres. Commit (mysqlxid, false );
System. Out. println ("MySQL transaction committed successfully! ");
Xasqlres. Commit (sqlxid, false );
System. Out. println ("sqlserver transaction submitted successfully! ");
Xamysqlres2.commit (mysqlxid2, false );
System. Out. println ("mysql2 transaction committed successfully! ");
Xaorares. Commit (oraxid, false );
System. Out. println ("Oracle transaction committed successfully! ");
} Else {
Xamysqlres. rollback (mysqlxid );
Xasqlres. rollback (sqlxid );
Xamysqlres2.rollback (mysqlxid2 );
Xaorares. rollback (oraxid );
System. Out. println ("transaction rollback successful! ");
}
} Catch (sqlexception ex ){
Logger. getlogger (mutil_performance_test.class.getname (). Log (level. Severe, null, ex );
Try {
Xamysqlres. rollback (mysqlxid );
Xasqlres. rollback (sqlxid );
Xamysqlres2.rollback (mysqlxid2 );
Xaorares. rollback (oraxid );
} Catch (xaexception e ){
System. Out. println ("rollback also failed !~ ");
E. printstacktrace ();
}
} Catch (xaexception ex ){
Logger. getlogger (mutil_performance_test.class.getname (). Log (level. Severe, null, ex );
} Finally {
Try {
// Close
Mysqlpst. Close ();
Mysqlcn. Close ();
Xamysqlcn. Close ();
Sqlpst. Close ();
Sqlcn. Close ();
Xasqlcn. Close ();
Mysqlpst2.close ();
Mysqlcn2.close ();
Xamysqlcn2.close ();
Orapst. Close ();
Limit N. Close ();
Xaoracn. Close ();
} Catch (sqlexception ex ){
Logger. getlogger (mutil_performance_test.class.getname (). Log (level. Severe, null, ex );
}
}
}
}
Distributed transactions are divided into two stages. The first stage is equivalent to a pre-commit, and the second stage is a real commit.
The first thing we need to implement is the XId interface. formatid can be understood as the ID of a global transaction. However, the code above does not handle exceptions and closes the correct link, it's just a small test done by myself, which will be processed later in the project. In the first stage, if an error occurs or the request fails, it will not be submitted. You need to perform some rollback. If yes, you are ready to submit the transaction. In the second stage, problems may occur. If the first branch transaction is committed successfully, then one branch transaction fails to be committed, this will result in inaccurate data. At present, there is no way to solve some problems, like xaresource. recover () can be processed, but you can find a solution in the project.
I am a beginner in XA distributed transactions, and I don't know anyone. I just want to find some information on the Internet. My understanding above may be somewhat inadequate or incorrect. Hope the experts can give you some advice.
- Xa_jarpacket .rar (2.6 MB)
- Downloads: 106