Canceling a transaction through API

119 views Asked by At

I'm developing a system that contains multiple microservices operating on the same database, which isn't ideal but is done for the sake of legacy support. As we refactored the old system we split old, transactional functionality into separate microservices, which led us to have distributed transactions now that must be handled in some way. We're using Spring boot.

As it usually goes, we have a microservice A calling microservices B and then C, we need a way to rollback transaction B if transaction C throws an error.

I've read on 2PC and Saga approaches, I was wondering if there is a way to implement a somewhat simpler variation of 2PC approach. It would need to support the following functionality.

1)Microservice endpoint is called, it calls a @Transactional service which creates a transaction. A special parameter is passed that tells the TransactionManager to keep the transaction "hanging" for some time (say, 5 seconds)

2)In those 5 seconds, another call can be made that either confirms or rolls back the transaction

3)If the time elapses (times out), default handling for the transaction is applied (either rollback or commit)

4)If the special parameter isn't passed in, transactions behave as if they are not distributed

5)API calls are still asynchronous

Simple example:

1)Service A endpoint "createResource" is called with parameter "hangTransaction=true"

2)Service A returns status 200

3)Service B endpoint "registerResource" is called

4)Service B returns status 500

5)Service A endpoint "createResource" is called with parameter "transactionValid=false"

6)Service A rolls the transaction back and returns status 200

7)There are no changes to DB after this

There would of course be additional parameters sent (transaction ID for example), the example is simplified.

Is there some way to control the TransactionManager in a way that allows the transaction to persist between API calls?

0

There are 0 answers