How handle transaction in Chain Of Responsibility pattern?

410 views Asked by At

I have a transaction issue on COR pattern:

       AbstractChainHandler
      |                   |
      |                   |
   FirstChainHandler     SecondChainHandler
    -create A               -create B
    -delete A               -delete B

FirstChainHandler and SecondChainHandler both extend AbstractChainHandler and they do some persistence tasks. Is it possible to handle transaction so if SecondChainHandler fails to save B on db also FirstChainHandler does the rollback of A persistence?

I am trying with spring @Transactional but it is not working and I am not sure if COR pattern matches my goal. I tryied to change propagation and isolation configurations but it didn't work.

2

There are 2 answers

0
iluwatar On

As a first option I would try to combine creation of A and B into single transaction. Maybe it's possible to handle transactions at the base class level? Another option is to combine the classes FirstChainHandler and SecondChainHandler into one. This would make it easy to handle the transactional part.

The second option adds complexity so I wouldn't go there if not absolutely necessary. With Saga and/or Compensating Transaction patterns you can achieve eventual consistency.

0
alex_noname On

I think Two-phase commit protocol is what you need to use.