The Java transaction was spoken yesterday because the transaction (transaction) is divided into two kinds
Local Transaction and Global Transaction
Involves a connection commit, called the local Transaction
A commit that involves multiple connection, called Global Transaction
Local transaction with JDBC Transaction implementation is no problem, but the implementation of the global transaction can not be guaranteed, so have to understand the magic of the next JTA, but in fact such a transaction is also problematic, the following will point out
XA requires two-phase commit-prepare and commit.
Assuming there are two connection, Con1, Con2, the general process is as follows
Con1 = xaresouce1.getconnection ... con2 = xaresouce2.getconnection ... con1 do some thing. Con2 do some thing. After they finish. Pre1 = Xaresouce1.prepare (); Pre2 = Xaresouce2.prepare (); if (both Pre1 and Pre2 are OK) {XAResouce1 and 2 commit}else {XAResouce1 and 2 rollback}
At the time of XAResouce1 and 2 commits,
Possibly XAResouce1 commit () succeeded, XAResouce2 commit () failed.
At this point, a "heuristic exception" is thrown. The program can handle this exception. For example, Xaresouce.recover ().
But in general, there is no other way, the data administrator needs to undo all operations based on the data operation log, or restore the data backup.
Some databases generate a "reverse operation" log when they perform data operations. For example, insert to delete, and so on.
The Global Transaction requires the support of the XA interface (included in JTA).
Import javax.sql.XAConnection;
Import Javax.transaction.xa.Xid;
Import Javax.transaction.xa.XAResource;
Import javax.transaction.xa.XAException;
Import javax.transaction.Transaction;
Import Javax.transaction.TransactionManager;
of which
Javax.sql.XAConnection;
Javax.transaction.xa.Xid;
Javax.transaction.xa.XAResource;
Implementations of these XA interfaces require JDBC provision of the database.
The database itself will support XA. Database JDBC also provides the implementation of XA.
Oracle, Sybase, DB2, SQL Server, and other large databases support XA
The free Postresql also supports XA
My SQL does not support the local transaction, let alone the global transation.
The implementation of TransactionManager can handle multiple xaresouce (a xaresouce list).
Like Tyrex. or the transaction implementation code of an EJB server such as JBoss
Reproduced below a detailed analysis of the principle of XA Http://jroller.com/pyrasun/category/XA a total of 3 parts, said very good, find a chance I will translate