FlushMode.AUTO后面检查什么?

发布于 2024-12-02 02:13:20 字数 83 浏览 1 评论 0原文

在Hibernate中,我想知道当flushMode为AUTO时哪些条件会触发刷新?它可能很复杂(或“神奇”),但基本条件是什么?

谢谢

In Hibernate, I wonder which conditions trigger flushing when flushMode is AUTO? It may be complex (or "magic") but what are the basic conditions?

Thanks

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

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

发布评论

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

评论(1

相守太难 2024-12-09 02:13:20

刷新模式FlushMode时。 AUTO 它将在以下时间发生:

  • 执行查询之前
  • 当 Hibernate API 上的事务提交时
  • 当应用程序显式调用 session.flush() 时

此模式的目的是避免“陈旧”状态。对内存中对象所做的更改可能与查询结果发生冲突。我的理解是 Hibernate 会进行“脏检查”,比较对象的原始状态和当前状态。如果它们不同,并且 Hibernate 认为这种差异会让您接触到过时的数据,它会尝试将此状态推送到数据库。这是可能的,因为 Hibernate 知道查询将“触及”哪些表以及需要针对当前脏状态更新哪些表。

看看这篇文章

尽管如此,自动冲洗在何时冲洗什么方面非常智能
有些情况可能很难发现。为了提高性能,
Hibernate 不会简单地总是刷新所有内容,而是看看
查询涉及的数据库中的表和内存中的数据
应该存储在这些表中。如果没有重叠,则没有数据
将会被坚持下去。如果发现重叠,则整个会话将被
冲洗,以确保一致性。

您还可以查看 org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush 源代码

FlushMode.AUTO 有几个缺点:

  • 您无法控制 Hibernate 何时决定执行 UPDATE/INSERT/DELETE
  • 潜在的性能问题,因为每个对象修改都可能导致脏检查 + DML 语句
  • 执行没有利用 Hibernate 在不试图避免“陈旧”状态时可以执行的批处理和其他优化的优势。

由于这些缺点,我更喜欢 FlushMode.COMMIT ,它可以为您提供更多控制。在这种模式下,Hibernate 会等待,直到您显式调用 Commit 或 Flush,然后才将内存中的状态与数据库同步。

When flush mode is FlushMode.AUTO it will happen at following times:

  • Before a query is executed
  • When a Transaction on the Hibernate API is committed
  • When the application calls session.flush() explicitly

The purpose of this mode is to avoid 'stale' state. Changes made to objects in memory may conflict with the results of the query. My understanding is that Hibernate will do a 'dirty-checking', compare original and current state of objects. If they are different and Hibernate thinks that this difference will expose you to stale data, it will try to push this state to database. It is possible because Hibernate knows what tables will be 'touched' by query and what tables it will need to update for current dirty state.

Take a look at this article:

Auto-flushing is quite intelligent in what to flush when, although
some situations might be hard to detect. To improve performance,
Hibernate will not simply always flush everything, but look at the
tables in the database that the query touches and the data in-memory
that should be stored in those tables. If there is no overlap, no data
will be persisted. If an overlap is found, the whole session will be
flushed, to ensure consistency.

You can also look at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush source code.

There are couple disadvantages of FlushMode.AUTO:

  • you don't control when Hibernate will decide to execute UPDATE/INSERT/DELETE
  • potential performance issues because every object modification may lead to dirty checking + DML statement execution
  • you are not taking advantage of batching and other optimizations that Hibernate can perform when it is not trying to avoid 'stale' state

Because of these disadvantages I prefer FlushMode.COMMIT which gives you more control. In this mode Hibernate waits until you explicitly call Commit or Flush and only synchronizes in-memory state with database after that.

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