Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does @Transactional annotation avoid concurrent access to business layer methods

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

like image 644
vigamage Avatar asked Jan 17 '18 09:01

vigamage


People also ask

What does the @transactional annotation do?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

Does @transactional work on protected methods?

As most of you probably know, due to the very nature of the solution, Spring's @Transactional annotation does not work on private methods unless you're using the AspectJ mode (which, in my experience, most of us aren't).

When should we use @transactional annotation?

The @Transactional annotation belongs to the Service layer because it is the Service layer's responsibility to define the transaction boundaries.

Can we use @transactional in service layer?

You should use @Transactional at service layer, if you want to change the domain model for client B where you have to provide the same data in a different model,you can change the domain model without impacting the DAO layer by providing a different service or by creating a interface and implementing the interface in ...


1 Answers

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.

like image 169
Kayaman Avatar answered Sep 18 '22 16:09

Kayaman