Dubbo Distributed Transactions

Source: Internet
Author: User
I think the management of affairs should not belong to the Dubbo framework,
Dubbo can only be managed by the transaction,
Like JDBC and JMS are distributed resources that can be managed by transactions.
Dubbo as long as the same behavior can be managed by the transaction, such as can be rolled back,
The scheduling of other transactions should be implemented by a dedicated transaction manager.

In Java, the main specification for distributed transactions is Jta/xa,
Where: JTA is the Java Transaction Manager specification,
XA is an industry-standard X/open CAE specification that can be defined by two-phase commit and ROLLBACK TRANSACTION resources,
For example, if a database implements the XA specification, then both JTA and MSDTC can transact the database based on the same behavior.

in Jta/xa, there are two main extension points:

(1) TransactionManager
JTA transaction Manager interface, implement this interface, can complete the transaction scheduling for all XA resources, such as Bea Tuxedo,jbossjta.

(2) XAResource
XA resource interface, the implementation of this interface, can be arbitrary transactionmanager scheduling, such as: JDBC XACONNECTION,JMS xamq and so on.

And Dubbo's remote service, also should be a xaresource, for example: Xainvoker and Xaexporter,
Dubbo only the first commit, the request is sent to the service provider for caching and writing,
On the second commit, the IMPL implementation of the service is then invoked based on the cache.
Of course, some of the healthy branching processes need to be considered clearly.

the basic principles of JTA/XA are as follows:



1. The user initiates a transaction:
Java code transactionmanager.begin ();

2. The transaction manager Initializes a transactional instance in the current thread:
Java Code threadlocal.set (new Transactionimpl ());

3. The user invokes a JDBC or JMS or Dubbo request, requesting an internal initialization of an Xaresource instance:
Java code XAResource XAResource = new Xaresourceimpl (); For example: xaconnection

4. Get transactions from the current thread inside JDBC or JMS or Dubbo:
Java code Transaction Transaction = Transactionmanager.gettransaction (); Its interior is: Threadlocal.get ();

5. Register the current Xaresource in the transaction:
Java code Transaction.enlistresource (XaResource);

6. The user submits a transaction:
Java code transactionmanager.commit (); The interior is: gettransaction (). commit ();

7. The transaction for loop invokes the two-phase commit of all registered Xaresource:
Java code Xid Xid = new Xidimpl ();   for (XAResource xaresource:xaresources) {xaresource.prepare (XID);   Xaresource.commit (XID, true);   Xaresource.commit (XID, false); }

8. There are, of course, some unusual processes, such as rollback and forget.

Example:
Java Code    transactionmanager transactionmanager = ...;  //  get    transactionmanager.begin (); //  start transaction    try  from Jndi Lookup {       jdbcconn.executeupdate (SQL); //  Execute SQL statement, DB writes Binlog, but does not update table         jmsmq.send (message); //  sends messages, MQ logs messages, but does not enter queues         dubboservice.invoke (parameters); //  call remote service, provider cache request information, but do not execute         transactionmanager.commit (); //  Commit TRANSACTION, database, message queue, remote service simultaneous commit   } catch (throwable  t)  {       transactionmanager.rollback (); //  rollback TRANSACTION, database, message queue, Remote service rollback at the same time   }   http://javatar.iteye.com/blog/981787

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.