Federico Piazza
Federico Piazza

Reputation: 30985

Shared transaction between multiple applications

we got a requirement of create a transactional operation but handled through multiple applications.

I'm familiar with @Transactional annotation to achieve transactions, or for example using it programmatically like:

@Autowired
private EntityManagerFactory emf;

public void doSomething() {
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.createNativeQuery("...do something...").executeUpdate();
    tx.commit();
    em.close();
}

So, if I need to allow an app to create a transaction dynamically I could pass em as parameter and populate multiple operations from different methods.

However, we got a weird requirement to achieve something like above code but involving multiple applications that share the same database, it would be like a distributed transaction.

Is this feasible with Spring? I'm not sure if I might create some rest services where one of the parameters are the serialized entity managers, although this looks very weird to me. Is it possible to handle like a shared transaction populating operations through multiple apps?

Upvotes: 0

Views: 1733

Answers (1)

Kostiantyn
Kostiantyn

Reputation: 1906

You may involve XA Transactions over JTA. You can use Spring support for JTA which implies use of particular JTA Transaction Manager implementation under the hood such as Atomikos (paid) or Bitronix (Apache-licensed, issues with maintenance now).

Some good overview is also available here, especially describing the alternatives to XA, including description of eventual consistency

Spring Boot + Atomikos sample on Github can demonstrate basics of JTA configuration within single application.

Once you get familiar with all of that, you may have similar question as in this thread. Note, that latter is raised 4 years ago and nothing has changed drastically since that time, so the answers there are still good.

In general, development and implementation of distributed ACID transactions across several applications/services can be a really challenging epic, often comparable by level of effort with re-architecting the whole solution in order to avoid XA usage at all.

Upvotes: 1

Related Questions