Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to use spring MVC with Hibernate in DAO, service layer architecture

I am using Spring MVC with Hibernatedaosupport for my DAO classes. Confused here where to start the transaction, whether it should be in service layer or DAO layer?

My view interacts with Service layer. DAO's are injected into services.

What is the right way to use Spring MVC with Hibernate in DAO, service layer architecture?

like image 569
Ramesh Kotha Avatar asked Jan 24 '12 20:01

Ramesh Kotha


1 Answers

IMHO the transactions should go to service layer. Typically one business transaction consists of several queries and updates. If you place @Transactional only on DAO layer, each query and update will run in a separate transaction, which effectively defeats the purpose of transactions.

But if services are @Transactional, each database interaction joins one main transaction started when web layer entered the service layer. Note that in this case if web layer runs several service methods, each of them will run in a separate transaction (the same problem shifted one level up). But placing @Transactional in web layer might introduce unexpected side effects like N+1 problem, which would have been caught otherwise. Thus try to keep one business transaction in one service method called from web layer.

like image 75
Tomasz Nurkiewicz Avatar answered Oct 05 '22 00:10

Tomasz Nurkiewicz