I spoke about Java transactions yesterday, because transaction is divided into two types:
Local transaction and global transaction
A connection-related commit is called a local transaction.
The commit involving multiple connections is called global transaction.
The implementation of local transaction with JDBC transactions is no problem, but the implementation of global transaction cannot be guaranteed, so we have to understand the magic of JTA, but in fact such transactions also have problems, it will be noted below
XA requires two-phase commit: prepare and commit.
Assume there are two connections, con1 and con2. The general process is as follows:
Con1 = xaresouce1.getconnection... <br/> con2 = xaresouce2.getconnection... </P> <p> con1 do some thing. <br/> con2 do some thing. <br/> after they finish. </P> <p> pre1 = xaresouce1.prepare (); <br/> pre2 = xaresouce2.prepare (); </P> <p> If (both pre1 and pre2 are OK) {<br/> xaresouce1 and 2 commit <br/>} else {<br/> xaresouce1 and 2 rollback <br/>}< br/>
When xaresouce1 and 2 commit,
Maybe xaresouce1 commit () is successful, and xaresouce2 commit () fails.
At this time, a "heuristic exception" will be thrown ".ProgramThis exception can be handled. For example, xaresouce. Recover.
However, in general, there is no other way. The data administrator needs to perform all the Undo operations based on the data operation log, or recover the data backup.
Some databases generate a "reverse operation" log when performing data operations. For example, insert to delete.
Global transaction requires support for XA interfaces (including 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;
The
Javax. SQL. xaconnection;
Javax. transaction. Xa. Xid;
Javax. transaction. Xa. xaresource;
The implementation of these XA interfaces requires the JDBC provision of the database.
The database itself must support Xa. Database JDBC must also provide XA implementation.
Large databases such as Oracle, Sybase, DB2, and SQL server support Xa.
Free postresql also supports XA
My SQL does not support local transaction, let alone Global transation.
The implementation of transactionmanager can handle multiple xaresouce (one xaresouce list) situations.
For example, tyrex. Or the transaction Implementation of EJB server such as JBossCode
Next reposted an explanation of Xa detailed principles of http://jroller.com/pyrasun/category/XA is divided into three parts, speak very good, find a chance I will translate