且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-09-12 19:47:28

恕我直言交易应到服务层。通常情况下一个业务交易由几个查询和更新的。如果你把 @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.