I'm developing a distributed web application with spring and hibernate. First of all let me explain the basic structure of the application.

Environment

We have 3 JEE web projects (.war),

1) Web UI

2) Purchase related service (back-end application)

3) Payment related service (back-end application)

All these applications are running in 3 different tomcat servers in 3 physical servers. Development database is PostgreSQL and Oracle 11G as live DB.

Scenario

When user enter new purchase details to the system I want to create a payment record too. Purchase record should handled by purchase back-end and payment should handled by the payment back-end. Currently everything works fine without the transaction handling part.

Problem

If something goes wrong in the payment back-end, purchase record also should be rollback. What is the best way to handle these distributed transactions. Can I use Narayana-JTS standalone version to fulfill my requirement. Can someone suggest a method and sample spring configuration code snippets.

Note

I prefer a method without JBossAS, because my applications are lightweight and no need any AS features up-to now.

Thank you very much for your quick reply. We can use WildFly AS but will not be able to use EJB because we have finished most of the development using Hibernate. Anyway I'll go through the documents and get back to you soon.

I would add a small EJB between the existing app and existing clients to propagate the transport.

Whatever approach you take you will need to write some more code, I suspect the EJB code would be smaller in comparison to the alternative but I may be wrong.

There are alternative methods of propagating the transaction, we have a webservices transport and a REST one. Let me know if you think these might help. There should be quickstarts for these in https://github.com/jbosstm/quickstart/

We have a similar problem. 2 applications on different war files that communicate via hessian protocol (based on HTTP, then very similar to REST) and we are trying to create a global transaction across the 2 applications.

Ah, I thought you were asking after REST examples. We don't have a hessian transport for JBossTM out of the box. Is there a transaction API for hessian do you know? The core of JBossTM is a pluggable architecture and we have support for many transports: JTS/SOAP/REST/JBoss Remoting. If there is some way to integrate hessian with REST stuff it might work.

It should certainly be possible but its not supported out of the box and would required you to write the transport adaptors. One of the best examples of this would probably be the way that the JBoss Remoting transport was written. You could take that as a template. They basically register as an XAResource and then serialize and ship the Xids around and respond to the transaction managers XAResource invocations. Special consideration is required at recovery.

It's in that repo I linked to above. Clone the repo and look for the XAResource implementations, iirc there are two, one for recovery and one for normal usage. Once you have found the XAR it should be relatively straightforward to see how the propagation is done for JBoss Remoting. The next thing is to see how the XAR are registered with the transaction manager. The recovery one is straight forward, the one that is registered when the transaction propagates is a bit more complex and would be the toughest bit for you to implement I suspect. With your application you will need to register a hook that anytime it propagates to another server you need to hook in an XAResource if its not already enlist for that TX at that server pair.