简体   繁体   English

使用persist()和save()休眠长时间运行的对话

[英]Hibernate long running conversations with persist() and save()

The Hibernate document says that persist() is useful compared to save() for long-running conversations with Session/Persistence context Hibernate文档指出,与save()相比, persist()对于与Session/Persistence context长时间运行的对话很有用。

persist() 坚持()

This is useful in long-running conversations with an extended Session/persistence context. 这在具有扩展会话/持久性上下文的长时间对话中很有用。

save() 救()

This is problematic in a long-running conversation with an extended Session/persistence context. 在具有扩展的会话/持久性上下文的长时间对话中,这是有问题的。

1) What it means when it says Persistence context ? 1)说Persistence context是什么意思?

2) Can you please provide an example on what are long-running conversations ? 2)您能否举例说明什么是long-running conversations Is this applicable for web applications? 这适用于Web应用程序吗?

Thanks! 谢谢!

Update: This post gave me clear understanding for my first query - Persistence context as: 更新: 这篇文章使我对第一个查询有了清晰的了解- Persistence context为:

At runtime whenever a session is opened and closed, between those open and close boundaries Hibernate maintains the object in a Persistence Context. 在运行时,只要打开和关闭会话,在这些打开和关闭边界之间,Hibernate就会在持久性上下文中维护对象。 Think of it like a first level runtime cache which hibernate controls. 可以将其视为休眠控件的一级运行时缓存。 Hibernate does automatic dirty checking and transactional write-behind for the entity managed in Persistent Context. Hibernate对持久性上下文中管理的实体进行自动的脏检查和事务后写。 Hibernate guarantee a scope of Java object identity in this cache. Hibernate保证此缓存中Java对象身份的范围。 Only one object instance representing a particular database row exists in the cache. 高速缓存中仅存在一个代表特定数据库行的对象实例。

A conversation is the interaction of the end user with the UI in order to perform a business task. 对话是最终用户与UI的交互,以执行业务任务。 This interaction will typically span many UI actions . 这种交互通常会跨越许多UI actions

Hibernate manages entities in a persistence context that is associated with a Hibernate session . Hibernate在与Hibernate session关联的persistence context中管理实体。 There are two patterns used to integrate a Hibernate back-end with a UI front-end: 有两种模式可用于将Hibernate后端与UI前端集成:

session per conversation : A hibernate session is created when a user begins the conversation and is kept alive until the user aborts or finishes the conversation. 每个会话的会话 :用户开始会话时会创建一个休眠会话,并保持活动状态,直到用户中止或结束会话。 When the latter happens, a transaction begins, the session is flushed and closed, thus sending SQL to the database(s) and the transaction is committed/rolled back. 当后者发生时,事务开始,会话被刷新并关闭,从而将SQL发送到数据库,事务被提交/回滚。

session per request (UI action) : A hibernate session is created when a user performs a UI action. 每个请求会话(UI操作) :用户执行UI操作时,将创建一个休眠会话。 For each UI action a transaction begins, the code involving this action is run, the session may be flushed and closed and the transaction is committed/rolled back. 对于每个事务开始的UI操作,将运行涉及该操作的代码,可以刷新和关闭会话,并提交/回滚事务。 The developer is responsible for re-attaching detached entities , previously managed in on the new session, if applicable. 开发人员负责re-attaching detached entities先前在新会话中管理的re-attaching detached entities (如果适用)。

The first pattern might look more appealing, but, in practice, especially when web applications are concerned the second is favored, because unless the UI is extremely simple, it is much easier to implement. 第一种模式可能看起来更有吸引力,但是在实践中,尤其是当涉及到Web应用程序时,第二种模式是受青睐的,因为除非UI非常简单,否则它很容易实现。 You might want to look at how to implement the open session in view pattern (another name for the session per request pattern) with a servlet filter or lookup the documentation of the web application framework of your choice on how it supports this. 您可能想看一下如何使用servlet过滤器open session in view模式(每个请求模式的会话的另一个名称)实现open session in view或者查找您选择的Web应用程序框架的文档以了解如何支持此方法。

Another thing to note is how to perform conversation isolation (similar concept with transaction isolation on databases). 要注意的另一件事是如何执行对话隔离(与数据库上的事务隔离类似的概念)。 This deals with the possibility that two users working simultaneously with the same data might override each others changes. 这解决了两个同时使用相同数据的用户可能会覆盖彼此的更改的可能性。 Have a look at optimistic locking with timestamps or versions. 看一下带有时间戳或版本的乐观锁定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM