1

I have several operations (service calls,but I guess that's not important) I need to run atomically. Let's say I got operations A, B and C which read and write DB multiple times and I need:

  • B to see DB changes that A did (and C to see what A and B did). This is important because some of the operations use results of previous operations.
  • Everyone else (say some other transactions) to NOT see the changes (e.g. if they read DB) until all three operations are finished and transaction is commited as a whole (or rolled back if something goes wrong). This is important because DB tables in question are not consistent until these operations are all finished.

I'm thinking @Transactional (like in example below) is exactly what I need here (with correctly configured isolation etc.), but I'm not sure. Can I use @Transactional to solve this? If yes, how to configure it correctly? Thanks.

@Transactional(...)
public void someTransactionalMethod(...) {
  callA();
  callB();
  callC();
}

P.S.: you may suggest that I try to design things a bit differently (better), but I'm afraid that's beyond the realm of possibility. I am bound by existing code etc. (for example inner workings of most of those operations are not under my control).

awa993
  • 177
  • 2
  • 14
  • You don't have to do anything else than putting \@Transactional on top of the method no any other config necessary. Or just to make sure you can use \@Transactional (isolation=Isolation.READ_COMMITTED). The reason is spring takes default from database engine. As far as I know for majority of engine it is read committed default but it would be wise to check. – cool Jun 26 '20 at 20:41
  • @cool I was hoping it would be as easy as that. Thank you. – awa993 Jun 27 '20 at 09:22

1 Answers1

1

Apparently @Transactional is exactly what I need here. Thanks @cool

awa993
  • 177
  • 2
  • 14