在DAO,服务层体系结构中,使用Spring MVC与Hibernate的正确方式是什么? [英] What is the right way to use spring MVC with Hibernate in DAO, service layer architecture
问题描述
我为我的DAO类使用了Spring MVC和 Hibernatedaosupport
。这里混淆了从哪里开始事务,它应该在服务层还是DAO层?
我的视图与Service层交互。 DAO的注入到服务中。
在DAO,服务层架构中使用Spring MVC与Hibernate的正确方式是什么?
恕我直言,交易应该转到服务层。通常,一个业务事务包含多个查询和更新。如果您仅在DAO层上放置 @Transactional
,则每个查询和更新都将在单独的事务中运行,这有效地破坏了事务的目的。
但是如果服务是 @Transactional
,每个数据库交互都会加入一个在web层进入服务层时启动的主事务。请注意,在这种情况下,如果Web层运行多个服务方法,它们中的每一个都将在单独的事务中运行(同一个问题将向上移动一级)。但是在web层中放置 @Transactional
可能会引入意想不到的副作用,例如N + 1问题,否则就会被捕获。因此,尽量保持一个业务交易在一个从web层调用的服务方法。
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?
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.
这篇关于在DAO,服务层体系结构中,使用Spring MVC与Hibernate的正确方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!