Distributed Transactions (JMS,DUBBO)

Source: Internet
Author: User
Tags commit message queue rollback

The distributed transaction about the Dubbo service framework, although it is not urgent to do, but can be discussed.

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 or three-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:

Transactionmanager.begin ();

2. The transaction manager Initializes a transactional instance in the current thread:

Threadlocal.set (New Transactionimpl ());

3. The user invokes a JDBC or JMS or Dubbo request, requesting an internal initialization of an Xaresource instance:

XAResource XAResource = new Xaresourceimpl (); For example: xaconnection

4. Get transactions from the current thread inside JDBC or JMS or Dubbo:

Transaction Transaction = Transactionmanager.gettransaction (); Its interior is: Threadlocal.get ();

5. Register the current Xaresource in the transaction:

Transaction.enlistresource (XaResource);

6. The user submits a transaction:

Transactionmanager.commit (); The interior is: gettransaction (). commit ();

7. The transaction for loop invokes the three-phase commit of all registered Xaresource:

Xid Xid = new Xidimpl ();
for (XAResource xaresource:xaresources) {
xaresource.prepare (XID);
}
for (XAResource xaresource:xaresources) {
xaresource.commit (XID, True);
}
for (XAResource xaresource:xaresources) {
xaresource.commit (XID, false);
}

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

Example:

TransactionManager TransactionManager = ...; Get Transactionmanager.begin () from Jndi for lookup, etc.//
start transaction
try {
jdbcconn.executeupdate (SQL);// Executes the SQL statement, DB writes Binlog, but does not update table
jmsmq.send (message);//sends messages, MQ logs messages, but does not enter queue
dubboservice.invoke (parameters); Call remote service, provider cache request information, but do not execute
transactionmanager.commit ();//COMMIT TRANSACTION, database, message queue, remote service commit at the same time
} catch (Throwable t) {
transactionmanager.rollback ();//ROLLBACK TRANSACTION, database, message queue, remote service rollback at the same time
}

Go from: http://www.cnblogs.com/MRRAOBX/articles/3127773.html

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.