“@Transactional"应该在哪里?放置服务层或DAO [英] Where should "@Transactional" be placed Service Layer or DAO

查看:82
本文介绍了“@Transactional"应该在哪里?放置服务层或DAO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我问的问题可能是之前有人问过和回答过的,但我无法返回搜索结果.我们在服务层上定义事务注解,典型的spring hibernate crud 通常是

Firstly it is possible that I am asking something that has been asked and answered before but I could not get a search result back. We define transactional annotations on service layer typical spring hibernate crud is usually

Controller->Manager->Dao->Orm .

我现在需要在基于客户端站点的域模型之间进行选择.假设客户端 A 使用我的域模型一切都很好,但其他客户端站点会给我一个网络服务,而不是使用我们的域模型.

I now have a situation where I need to choose between the domain model based on client site. Say client A is using my domain model all is good but then an other client site would give me a web service and not be using our domain model.

我应该替换哪个层.我相信它必须是 DAO,它将从 Web 服务获取数据并将其发送回.即两个单独编写的 DAO 层并根据场景插入.

Which layer should I be replacing . I believe it has to be DAO which will be getting me data from web service and sending it back.i.e two separately written DAO layers and plugged in based on scenario.

我现在意识到当我们把@Transactional放在Service层时,我们一直在做紧耦合(如果有这种事情或者说没有松散耦合).这么多大脑不可能是错的,或者他们是(我怀疑).

I have now realized that we have been doing tight coupling (if there is such a thing or say not having loose coupling) when we put @Transactional in Service layer. So many brains can not be wrong or are they (I doubt it).

所以问题是应该在哪里@Transactional"?放在服务层还是DAO?"是否应该更换向下的服务层.

So question is "Where should "@Transactional" be placed Service Layer or DAO ?" and is it service layer downwards I should be replacing.

11 年过去了,仍然具有相关性.如果我回顾这个项目,我当时对领域模型的理解显然有问题.我将 ORM 层视为域模型,我们希望使用 ORM 和分离的实体,并且没有任何数据映射,也没有任何 DTO.那是那个年代的趋势.现在领域模型不是 ORM,拥有适当的领域模型和使用 ORM 或 Web 服务是数据源来解决这个问题.就像许多人指出的那样,服务是正确的地方,并且具有适当的领域模型,而不是将 JPA (ORM) 视为领域模型.

Eleven years on and still relevant . If I look back at the project somethings were obviously wrong with my understanding of Domain model back then . I was regarding ORM layer as domain model and we wanted to work with ORM and detached entities and no have any data mapping and not have any DTOs. That was the trend those days. These days Domain Model is not the ORM and having a proper Domain model and using ORM or Webservices are datasources take care of this issue. Like many pointed out yes Service is the right place for it and have proper domain model and not regard JPA (ORM) as domain model.

推荐答案

理想情况下,服务层 (Manager) 代表您的业务逻辑,因此应使用 @Transactional.

Ideally, Service layer (Manager) represents your business logic and hence it should be annotated with @Transactional.

Service 层可能会调用不同的 DAO 来执行 DB 操作.让我们假设在一个服务方法中有 3 个 DAO 操作的情况.如果您的第一个 DAO 操作失败,则其他两个可能仍会通过,您最终会得到不一致的 DB 状态.注释服务层可以避免这种情况.

Service layer may call different DAOs to perform DB operations. Lets assume a situation where you have 3 DAO operations in a service method. If your 1st DAO operation failed, other two may be still passed and you will end up with an inconsistent DB state. Annotating Service layer can save you from such situations.

这篇关于“@Transactional"应该在哪里?放置服务层或DAO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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