SpringBoot & nested transaction isolation level override

Viewed 200

Spring boot, jdbc.

I feel a bit slippery when it comes to nested transaction..

Say there is MyService that uses MyRepository.

    @Named 
    @Transactional(isolation=Isolation.READ_COMMITTED) // READ_COMMITTED < SERIALIZABLE
    class MyService {
      void do() {
        myRepository.add(entity);
         ...
      }
    }
    @Repository
    class MyRepository {
         @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.SERIALIZABLE)
         void add(entity) {...}

    }

Say, in this case i want to make sure that MyRepository takes care about the add() method, making sure it set a maximum level lock Isolation.SERIALIZABLE over the table it deals with.

I do not want anyone to override it, since it would lead to concurrency issues.

(same applies to propagation level)

Question:

In case of nested transaction, will transaction's of myService.do() override Isolation level of myRepository.add and maker it less strict ?

What are the precautions when dealing with nested transactions?

1 Answers

Hope this helps: As per java docs of @Transactional annotation:

The transaction isolation level. Defaults to Isolation.DEFAULT. Exclusively designed for use with Propagation.REQUIRED or Propagation.REQUIRES_NEW since it only applies to newly started transactions. Consider switching the "validateExistingTransactions" flag to "true" on your transaction manager if you'd like isolation level declarations to get rejected when participating in an existing transaction with a different isolation level.

Related