org.hibernate.Session.clear()认为有害? [英] org.hibernate.Session.clear() considered Harmful?

查看:154
本文介绍了org.hibernate.Session.clear()认为有害?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个设计问题,没有提交保护我的底部的具体代码。

This is a design question, concrete code not submitted to protect my bottom.

使用Hibernate时,标准工作流程如下:

When working with Hibernate the standard workflow is as follows:


  1. 开立会话

  2. 开始交易

  3. 做生意(阅读和修改数据)

  4. 提交交易

  5. 关闭会话

  1. Open Session
  2. Start Transaction
  3. Do the business (read and modify data)
  4. Commit Transaction
  5. Close Session

可能通过2-4迭代。

Session.clear()的合理用例是什么?

What are reasonable use cases for Session.clear()?

A:具体的问题是加载和修改实体的(大)代码,然后清除()会话,基本上抛弃了所做的更改。 (要完成的业务任务不包括修改实体,因此代码工作)。

A: The concrete problem I have is a (big) piece of code which loads and modifies entities, then clear()s the session, essentially throwing away the changes that were made. (The business task to be accomplished does not include modifying the entities, so the code "works").

似乎我认为正确的设计是确保(大块)代码不会改变它不想保存?

Seems to me the right design would be to make sure the (big) piece of code does not make changes it does not want to save?

B:我会猜测Session.clear()存在为了方便/灵活性,而不是因为它是一个好主意,使用它。

B: I would guess Session.clear() exists for convenience/flexibility, not because it is a good idea to use it.

我误解了Hibernate的理念吗?

Have I misunderstood the Hibernate philosophy?

C:Subquestion:框架代码在任务完成时无条件地清除()会话是一个坏主意吗? IMHO,框架应该抱怨当任务完成时会话是否脏?应该关闭会议,看待任务完成...(忽略分钟的表现)

C: Subquestion: Is it a bad idea for framework code to unconditionally clear() the session when a task completes? IMHO, the framework should complain if the session is dirty when a task completes! The session should be closed, seeing as the task is done... (Disregarding performance for the minute)

(标签A,B和C,所以你可以指出哪一部分你正在回答)。

(Labels A, B and C so you can indicate which part you are answering).

推荐答案

广告。 A :看起来你知道什么是 clear()。明确调用它的原因是从L1缓存中删除所有管理实体,以便在一个事务中处理大数据集时无限制地增长。

Ad. A: Looks like you are aware what clear() does. The reason to call it explicitly is to remove all managed entities from L1 cache, so that it does not grow infinitely when processing large data sets in one transaction.

它丢弃所有对受管理的项目进行的更改未明确持久化。这意味着您可以安全地修改实体,明确更新它并清除会话。这是右侧设计。显然,如果没有更改(长,但只读会话), clear()始终是安全的。

It discards all the changes that were made to managed entites not explicitly persisted. This means that you can safely modify an entity, update it explicitly and clear the session. This is the right design. Obviously if no changes are made (long, but read only session), clear() is always safe.

您还可以使用无状态会话

广告B :不,由于上述原因存在:确保L1(会话缓存)不增长太多。当然,手动维护它是一个糟糕的想法,并表明另一个工具应该用于大型数据集,但有时是必须的。

Ad. B: No, it exists for the reasons above: to make sure L1 (session cache) does not grow too much. Of course maintaining it manually is a poor idea and an indication that another tool should be used for large data sets, but sometimes it is a must.

请注意,在JPA规范中还有 clear() flush()方法。在这种情况下,您应该先调用 flush()在调用之前将更改推送到数据库(显式更新)clear()

Note that in JPA specification there is also clear() and flush() method. In this case you should always call flush() first to push changes into the database (explicit update) prior to calling clear().

广告。 C :事实上,当他/她清除会话时,警告用户(可能通过发出警告消息而不是抛出异常)是一个好主意。另外,我不认为框架代码应该无条件地调用 clear(),除非确定用户代码运行时刷新或不进行任何更改。

Ad. C: It's actually a good idea to warn the user (maybe by issuing warning message rather than throwing an exception) when he/she clears the session with dirty changes. Also I don't think a framework code should call clear() unconditionally, unless it is sure that the user code it runs flushes or does not make any changes.

这篇关于org.hibernate.Session.clear()认为有害?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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