解除引用的休眠 (JPA) 实体会发生什么情况?

发布于 2024-09-12 13:27:02 字数 579 浏览 6 评论 0原文

在我正在从事的一个项目中,我们有一个 EJB 后端,各种客户端远程连接(即 Web 层、Web 服务层等)。客户端位于另一台计算机上,并且可以位于另一个数据中心中,因此前端和后端永远不会位于同一应用程序服务器中。

后端分层如下:

SLSB <->服务层对象<-> DAO

所有对象都是 spring 管理的,除了 SLSB。事件链如下:

初始化:

  1. 实体管理器注入 DAO
  2. DAO 注入服务对象
  3. 服务对象注入 SL EJB
  4. SLSB 仅提供远程接口 所有对象都是单例和无状态

请求/响应:

在 EJB 上调用的方法、委托给服务对象、使用 DAO、返回 DTO

DAO 封装了 JPA 实体上的所有查询操作。没有 JPA 实体会渗透到服务层。服务层界定事务。

一旦请求/响应生命周期在此架构中完成,JPA 实体会发生什么?服务层是否应该尝试缓存实体,或者这是休眠的工作?

欢迎对此架构提出任何评论。

谢谢比尔沃斯

In a project i am working on, we have an EJB backend where various clients connect remotely (i.e. Web layer, web services layer, etc). The clients are on another machine and can be in another data center, so the front end and backend are never in the same app server.

The backend is layered as follows:

SLSB <-> Service Layer Objects <-> DAO

All objects are spring managed, except for the SLSB. The chain of events is as follows:

Initialization:

  1. Entity Manager injected into DAO
  2. DAO injected into Service Object
  3. Service Object injected into SL EJB
  4. SLSB's only provide a remote interface
    All objects are Singleton and stateless

Request/Response:

method invoked on EJB, delegates to Service Object, uses DAO's, return DTO

The DAO's encapsulate all the query operations on JPA entities. No JPA entity bleeds past the service layer. The service layer demarcates the transaction.

What happens to the JPA entities once the request/response lifecycle is complete with this architecture? Should the service layer attempt to cache the entities, or is that hibernates job?

And any comments on this architecture is welcome.

thanks

  • Billworth

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

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

发布评论

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

评论(1

旧情别恋 2024-09-19 13:27:02

一旦请求/响应生命周期在此架构中完成,JPA 实体会发生什么?

对于 TRANSACTION 范围内的容器管理的持久性上下文,当关联的 JTA 事务提交或回滚并且持久性上下文中的所有实体都被分离时,持久性上下文就会结束。来自 JPA 规范:

5.6.1 容器管理的事务范围持久化上下文

应用程序可能会获得
容器管理的实体管理器
事务范围的持久化上下文
绑定到 JTA 事务
在 JNDI 中注入或直接查找
命名空间。持久化上下文
实体管理器的类型是
默认或定义为
PersistenceContextType.TRANSACTION

一个新的持久化上下文开始于
容器管理的实体管理器
在范围内调用[36]
一个活跃的 JTA 事务,以及
没有现在的坚持
上下文已经与
JTA 交易。坚持
创建上下文然后关联
使用 JTA 事务。

持久化上下文结束时
关联的 JTA 事务提交或
回滚,并且所有实体
由EntityManager管理成为
分离。

如果实体管理器被调用
交易范围之外,
从数据库加载的任何实体
将立即脱离
方法调用结束。

如果应用程序不再保留引用,则分离的实体将被垃圾收集。

服务层是否应该尝试缓存实体,或者这是休眠工作?

如果您想要跨各种持久性上下文缓存实体,即二级 (L2) 缓存,那么这就是 JPA 提供程序的工作。它了解各种持久性事件并可以与缓存进行适当的交互。当您的 JPA 提供商已经提供此功能时,在服务层实现类似的机制是没有意义的。对于 Hibernate,请参阅 19.2。二级缓存

What happens to the JPA entities once the request/response lifecycle is complete with this architecture?

In the case of a container-managed persistence context that is TRANSACTION scoped, the persistence context ends when the associated JTA transaction commits or rolls back and all entities that were in the persistence context are detached. From the JPA specification:

5.6.1 Container-managed Transaction-scoped Persistence Context

The application may obtain a
container-managed entity manager with
transaction-scoped persistence context
bound to the JTA transaction by
injection or direct lookup in the JNDI
namespace. The persistence context
type for the entity manager is
defaulted or defined as
PersistenceContextType.TRANSACTION.

A new persistence context begins when
the container-managed entity manager
is invoked[36] in the scope
of an active JTA transaction, and
there is no current persistence
context already associated with the
JTA transaction. The persistence
context is created and then associated
with the JTA transaction.

The persistence context ends when the
associated JTA transaction commits or
rolls back, and all entities that were
managed by the EntityManager become
detached.

If the entity manager is invoked
outside the scope of a transaction,
any entities loaded from the database
will immediately become detached at
the end of the method call.

Detached entities will then be garbage collected if the application doesn't hold a reference anymore.

Should the service layer attempt to cache the entities, or is that hibernates job?

If you want to cache entities across various persistence contexts, aka second level (L2) caching, that's a job for the JPA provider. It is aware of the various persistence events and can interact appropriately with a cache. There is no point at implementing a similar mechanism at the service layer level when your JPA provider already offers this feature. For Hibernate, see 19.2. The Second Level Cache.

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