I have seen many comments in stack-overflow articles I found certain things about either @Transactional use with @Service or with @Controller
"Usually, one should put a transaction at the service layer."
"The normal case would be to annotate on a service layer level"
"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." [Source]
Drawback to use @transactional with @service layer
If I had 2 methods for example saveUser() and saveEmail() (because I store the emails in a database to send them later - like a queue) I would create in my service a method saveUserAndSendEmail(User user) which would be transactional. [Source]
It means I create many methods in service layer instead of one Save Generic Method as follow
public <T> long save(T entity) throws DataAccessException {
Session session = sessionFactory.getCurrentSession();
long getGenVal=(Long) session.save(entity);
return getGenVal;
}
According to the above solution , it means we have many methods like following LOL..
public <T> long saveAccount(T entity)....
public <T> long saveWithAuditLog(T entity, K entity1)....
public <T> long saveWithAuditLogAndEntries(T entity, K entity, M entity)....
OVERCOME this situation
I USE THE @Transactional in @Controller and Just make a Generic Save Method and save all the entities/ model using this simple save method. and if any method fail to save then all the transactions in controller rollback successfully.
Other situation that ensure that @Transactional should be use with @Controller
In @Controller:
pt.save(entity1);
pt.save(entity2);
int a = 2/0;
pt.save(entity3);
In case , @Transactional on Service, first 2 entity successfully saved but third not it not rollback all the transaction
In case , @Transactional on @Controller all the transaction rollback as exception occur
why stack-overflow asked , "Don't do transactions in your controller. Put them in your service layer classes."?
[source]
See Question&Answers more detail:
os