This tech tip explores some patterns for distributed transactions.
Suppose you have two processes (A and B) on two different machines. How can you ensure reliability of tasks that span these two processes? In other words: how can you make sure that there is no partial work done by A (but not by B) or the other way around. Depending on what you want and the nature of your application, there are different ways of doing this. This tech tip explains the most common solutions.
This solution applies to RMI-IIOP or web service (SOAP) scenarios.
In this approach, you would do the following:
This ensures that both A and B either commit or rollback the same way.
Another approach would be to use JMS, as explained next.
In this approach, the calls from A to B are done by JMS messages. This is done as explained below.
To send a message to B, A does the following:
On the other side, B receives a message as follows:
This pattern also ensures reliability, but should be used only under the following conditions:
The first condition is required to avoid message loss after A commits. The second condition is needed because JMS is asynchronous in nature. The third condition ensures that B will eventually be able to process the message. The fourth condition is due to the fact that JMS can't receive a reply to a message that is sent within the same transaction.
|For shared database situations between A and B: make sure to send (from A to B) via a different AtomikosConnectionFactoryBean, or (alternatively) make sure to send to B in a subtransaction. Otherwise, B might receive the message before A's database updates are committed… Also see Commit Ordering with JMS for details.|
This approach works similar to the first (RMI-IIOP) approach, except that JMS messages are used instead of RMI calls. In this case, the JMS messages can be either persistent or non-persistent, and the acknowledgement mode should NOT be transactional (meaning that non-XA, regular JMS drivers should be used and messages should be sent with either AUTO ACKNOWLEDGE or EXPLICIT ACKNOWLEDGE settings. If this is true then the following will work: