Hayi
Hayi

Reputation: 6256

Not using @Transactional and calling persistence layer methods

I have my services like this annotated with @Transactional

@Transactional
@Service
public class Contact_Service {
   ....

In my Controller i don't have @Transactional and sometimes i use some persistence layer methods directely to search and persist my object and everything is okey :

@Controller
public class GestionAO {

    ....

    @RequestMapping(...) 
    public String alerte() {

                contact_respository.findOne(...) ;
                ...
                contact_respository.save(...) ;

My question since my controller is not in a transaction will i got some problems ?
Can my object don't get saved in some cases ?
Will i got concurrence problem ?

Upvotes: 2

Views: 449

Answers (3)

Jeevan Patil
Jeevan Patil

Reputation: 6089

The purpose of having @Transactional annotation is to perform multiple database operations in a single transaction. If one operation fails, entire transaction should be roll-backed. Otherwise this can cause data integrity issues.

And the most important thing is to have @Transactional on service layer methods. Because that's one layer who knows unit of work and use cases. In some cases you will have to call multiple DAO layer methods, those all operations are called from service layer and mostly from single method. So it is always better to annotate your service layer methods as @Transactional.

To sum it up, You must have @Transactional annotations on service layer methods.

Upvotes: 4

Septem
Septem

Reputation: 3622

you should only annotate service with @Transactional. to make sure all of db operations under single transaction, it is recommended to add a new method in service which contains all you db operations, and just call this new method in controller.

Upvotes: 2

Andy Dufresne
Andy Dufresne

Reputation: 6190

Looks fine now when you have only one save call. If there are multiple DML or DDL operations executed from the Controller you will lose on not having transaction management. You will lose on the ACID behavior that transactions offer.

Upvotes: 4

Related Questions