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

查看:607
本文介绍了"@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 . Okay generally (or always so far:) ) We define transactional annotations on service layer typical spring hibernate crud is usually

Controller-> Manager-> Dao-> Orm.

我现在遇到一种情况,我需要在基于客户端站点的域模型之间进行选择. 假设客户端A使用我的域模型一切都很好,但是另一个客户端站点将为我提供Web服务,而不使用我们的域模型.

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 place Service Layer or DAO ?" and is it service layer downwards I should be replacing .

推荐答案

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

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

服务层可以调用不同的DAO来执行数据库操作.让我们假设一个服务方法中有3个DAO操作的情况.如果您的第一个DAO操作失败,则其他两个操作可能仍会通过,并且最终将导致数据库状态不一致.注释服务层可以帮助您避免此类情况的发生.

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天全站免登陆