什么是使用Spring MVC的与Hibernate的DAO,服务队层架构的正确方法 [英] What is the right way to use spring MVC with Hibernate in DAO, sevice layer architecture

查看:156
本文介绍了什么是使用Spring MVC的与Hibernate的DAO,服务队层架构的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring MVC中以的HibernateDaoSupport 我的DAO类。糊涂这里哪里开始交易,是否应该在服务层或DAO层?

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?

我的看法与服务层交互。 DAO的被注入到服务

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

什么是DAO使用Spring MVC与Hibernate的正确方法,服务层架构?

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

推荐答案

恕我直言交易应到服务层。通常情况下一个业务交易由几个查询和更新的。如果你把 @Transactional 只DAO层上,每个查询和更新将在一个单独的事务,从而有效地违背了交易的目的运行。

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.

但如果服务 @Transactional ,每个数据库的交互联接网络时,进入层服务层一个主交易开始。注意,在这种情况下,如果网络层运行多种服务方式,他们每个人都会在一个单独的事务中运行(同样的问题转向一个级别)。但是,在网络层将 @Transactional 可能会引入意想不到的副作用,如N + 1的问题,这会被抓,否则。因此,尽量保持一个业务交易,从网络层称为一个服务的方法。

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.

这篇关于什么是使用Spring MVC的与Hibernate的DAO,服务队层架构的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆