多个 DAO 实例 = 不好?

发布于 2024-12-13 11:30:46 字数 380 浏览 0 评论 0原文

两个应用程序(A 和 B)使用 hibernate 从数据库中检索相同的实体。是否应该在每个应用程序中实例化 DAO,还是应该有一个单独的应用程序 (C),其中包含 DAO 的唯一实例并公开服务(例如 RMI)供 A 和 B 使用?

如果后一种情况属实,那么防止加载异常的常见做法是什么?我认为我目前的计划是使用 RMI 并为每个域对象创建一个 DTO。我唯一的保留意见是 A 和 B 中的实体将无法引用 C 中的实体。这是常见策略吗?

可能值得一提的是,有 4 个不同的服务器,每个服务器都运行 A 和 B。目前,每个服务器上也都有数据库 - 出现这个问题是因为我试图集中某些东西 - 要么所有项目都有一个指向的 DAO单个数据库服务器或所有应用程序都指向由 C 托管的服务,该服务具有指向单个数据库的 DAO。

Two application (A and B) use hibernate to retrieve the same entities out of a database. Should a DAO be instantiated in each application or should there be a separate application (C) that contains the only instance of the DAO and exposes a service (e.g. RMI) for A and B to use?

If the latter case is true then what is the common practise for preventing loading exceptions? I think my plan currently is to use RMI and create a DTO for each domain object. My only reservation is that entities in A and B won't be able to reference entities in C. Is this a common strategy?

Something that might be worth mentioning is that there a 4 different servers each running A and B. Currently each server has the DB on it too - this question came about because I'm trying to centralise something - either all projects have a DAO pointing to a single DB server or all applications point to a service hosted by C that has a DAO to a single DB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红ご颜醉 2024-12-20 11:30:46

DAO 通常是一个无状态的单例,用于从数据库中加载、搜索、修改和删除实体。为什么要使用 RMI 与另一个 JVM 进行通信才能做到这一点?您将严重降低性能,实体永远不会附加到会话,并且您无法在单个实体上使用跨越多个操作(加载、存储)的事务。这根本没有意义。

每个应用程序中都有一个 DAO 实例。

A DAO is typically a stateless singleton used to load, search, modify and delete entities from the database. Why would you want to use RMI to communicate with another JVM just to do that? You would seriously decrease the performance, the entities would never be attached to the session, and you couldn't use transactions spanning more than a single operation (load, store) on a single entity. It just doesn't make sense.

Have a DAO instance in each of your application.

沧笙踏歌 2024-12-20 11:30:46

您可以使用 spring 并将此 DAO 实例 bean 添加到两个项目的父应用程序上下文中。

You can use spring and add this DAO instance bean to the parent application context for both of projects.

深者入戏 2024-12-20 11:30:46

这取决于你的架构。这两种方法都是可行的,并且各有优缺点。
如果您有一个 DAO 并通过 RMI / REST 连接到它,那么您已经使用 hibernate 中的项目解耦,但引入了一些延迟,对实体强加了可串行性并出现单点故障(可以通过集群来解决)

如果您每个项目都有自己的DAO,您从休眠配置中获得了复杂性,但避免了延迟和单点故障。

在这两种情况下,您都应该采取预防措施来防止并发数据修改并保持一致性(例如乐观锁定等)

It depends on your achitecture. Both approaches are possible and have advantages and disadvantages.
If you have one DAO and connecto to it via RMI / REST, you have decoupled using projects from hibernate, but introduced some latency, imposed serializability on entities and got single point of failure (can be solved by clustering )

If you each proejct has own DAO, you got come complexity from hibernate configuration but avoided latency and sngle point of failure.

In both cases you shall take precautions to prevent condurent data modifcations and keep consistency ( like optimistic locking etc. )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文