Search code examples
javaspringdatabasespring-transactionsacid

Using Spring @Transactional annotation


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).


Solution

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