Home > Software engineering >  Issue With Spring @Transactional Annotation
Issue With Spring @Transactional Annotation

Time:11-10

I am using Spring's @Transactional annotation to maintain the transaction in application but suddenly on one good day we started seeing an issue in application which was stated as below through application calls:-

@Transactional
Public void methodA()
{
 methodB();
}

public void methodB()
{
methodC();
}

public void methodC()
{
 Calling JMSTemplate.send();
}

So whenever issue occurs all the operation done in methodA and methodB gets rolledback but the IBM MQ send operation doesn't gets rolledback thus causing the issue in application: Can anyone Suggest what is good way to handle this scenario:

We tried to go through some documentation of @Transactional but didn't found anything concrete

CodePudding user response:

@Transactional is an annotation provided by the Spring framework to maintain a transactional execution at the database level. Hence, any changes done to the database can be rolled back. However, you cannot roll back a call to another service or a Message published to a message broker using this annotation.

I suggest you publish the message to the MQ after the completion of all database-related operations and business logic. In case that's not possible, you may follow the Design pattern like 'SAGA' in order to solve this consistency issue. SAGA pattern: https://microservices.io/patterns/data/saga.html

CodePudding user response:

That's called XA transaction management, you can read https://dzone.com/articles/xa-transactions-2-phase-commit as introduction to the problematic.

  • Related