5
votes

I am bit of unclear about the usage of @Transactional annotation in the business/service layer.

My question is, when used together with ISOLATION_SERIALIZABLE in business layer, does the @Transactional annotation guaranty that no concurrent access is allowed to the particular method?

@Transactional(isolation = Isolation.SERIALIZABLE)
public void businessMethod() {

    // calls subBusinessMethod
    subBusinessMethod();
    ---------------
    ---------------
    ---------------

}

Imagine that subBusinessMethod calls to the DAO layer to do some db transactions. The code execution(more db calls may be) after the call to subBusinessMethod depends on the result of the db transaction took place in subBusinessMethod.

I need to make sure that the subsequent calls to the businessMethod should not call the dao layer to read/write from/to the database without knowing what happened to the db tables from previous calls. Required validations have been added in the business logic.

First of all, is my approach valid? If so, am I using it in the correct way. ?

1

1 Answers

5
votes

No. The @Transactional attribute maps directly down to the database level. The method can and will be accessed concurrently (only synchronized or other locking can prevent that).

The underlying database transactions on the other hand will be executed with serializable isolation, i.e. the end results of the transactions must be as if each transaction were run one after another (even if they weren't really run one after another).

Based on your requirements, it seems that this is a viable approach. But it may not be the only or even the best one.