Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distributed transactions across Spring web applications

Imagine a Java ecosystem where three separate Spring web application is running in separate JVMs and on separate machines (no application server involved, just simple servlet containers). Two of these applications are using their own database accessed using JPA. Now the third application (a coordinator) provides services to the outside world and some service function executes remote operations which requires participation from the other two apps in a transactional manner, which means that if one of the applications fails to do the data manipulation in the database, the other should be rolled back as well. The problem is: how can this be achieved using Spring?

Currently we are using REST to communicate between the applications. Clearly this cannot support transactions, even though there are efforts to make this happen.

I've found JTA which is capable of organizing global transactions. JTA involves creating XAResource instances which are participating in the globally managed transactions. If i understood correctly, these XAResource instance can reside on separate JVMs. Initialization, commit and rollback of resources happens via JMS communication which means it requires a message broker to transfer messages between participants. There are various JTA implementation exists, I've found Atomikos which seems to be the most used.

Now the thing i don't see is how this all comes up if i have a Spring application on each application side. I've not found any example projects yet which is doing JTA over a network. Also i don't undertstand what are XAResources representing. If i use JPA, and say i have a Account object in an application which stores a user's balance, and i have to decrease the balance from the coordinator, should i create an XAResource implementation which allows decreasing the balance? Or XAResource is implemented by a lower level thing like the JDBC driver or Spring Data JPA? In the latter case how can i provide high level CRUD operations for the transaction coordinator.

like image 326
NagyI Avatar asked Jul 24 '14 14:07

NagyI


People also ask

What is distributed transaction in spring?

Spring Boot supports distributed JTA transactions across multiple XA resources by using either an Atomikos or Bitronix embedded transaction manager. JTA transactions are also supported when deploying to a suitable Java EE Application Server.

Can I use multiple transaction managers within a Spring application?

Can I use multiple transaction managers within a spring application? Yes. @Transactional annotation has the property transactionManager which can be set to specify which transaction manager to be used.

How do you handle distributed transactions in Microservices?

One of the important participants in a distributed transaction is the transaction coordinator. The distributed transaction consists of two steps: Prepare phase — during this phase, all participants of the transaction prepare for commit and notify the coordinator that they are ready to complete the transaction.


1 Answers

XAResource is a lower level API. You could write your own for the coordinator, but I think that isn't necesary. Instead, leverage JMS + JTA on the coordinator and JTA on the app servers.

In the normal case, you'd have this:

  1. Coordinator receives request and starts JTA transaction
  2. Coordinator calls app 1 over JMS
    • App 1 receives JMS message
    • App 1 calls DB 1 using JTA
  3. Coordinator calls app 2 over JMS
    • App 2 receives JVM message
    • App 2 calls DB 2 using JTA
  4. Coordinator commits tx

Note that JTA is used for all the transactions - this will be a global TX that's shared across all the servers. If any of these steps fail, then they will be rolled back.

Spring should be able to make this transparent once you get it all set up. Just make sure your DAO & service calls are transactional. Atomikos will need to be configured so that each server uses the same JTA tx manager.

like image 105
AngerClown Avatar answered Sep 28 '22 04:09

AngerClown