Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the @Transactional annotation belong?

Should you place the @Transactional in the DAO classes and/or their methods or is it better to annotate the Service classes which are calling using the DAO objects? Or does it make sense to annotate both "layers"?

like image 857
Thomas Einwaller Avatar asked Jul 03 '09 12:07

Thomas Einwaller


People also ask

What is @transactional annotation in Java?

Transactional annotation provides the application the ability to declaratively control transaction boundaries on CDI managed beans, as well as classes defined as managed beans by the Java EE specification, at both the class and method level where method level annotations override those at the class level.

What is use of @transactional annotation in Spring?

One of the most essential parts of Spring MVC is the @Transactional annotation, which provides broad support for transaction management and allows developers to concentrate on business logic rather than worrying about data integrity in the event of system failures.

Where do you put transactional?

While transactions management is implementation detail in many cases quite often it's an interface detail as well. For example, when defining interface of services of your application you might consider putting @Transactional into interface definition to specifically clarify what propagation strategy you're using.

What is @transactional annotation in Spring MVC?

The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics; for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction".


1 Answers

I think transactions belong on the service layer. It's the one that knows about units of work and use cases. It's the right answer if you have several DAOs injected into a service that need to work together in a single transaction.

like image 57
duffymo Avatar answered Sep 17 '22 13:09

duffymo