在FlushMode.AUTO后面检查什么? [英] What is checked behind FlushMode.AUTO?

查看:190
本文介绍了在FlushMode.AUTO后面检查什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Hibernate中,我想知道当flushMode是AUTO时哪些条件触发刷新?它可能是复杂的(或魔术),但基本条件是什么?

谢谢 class解决方案方案

刷新模式

a>是 FlushMode.AUTO 它会在以下时间发生:


  • 查询已执行

  • 当提交Hibernate API上的事务时
  • 当应用程序明确调用session.flush()时



这种模式的目的是避免陈旧状态。对内存中对象所做的更改可能与查询结果相冲突。我的理解是,Hibernate会做一个'脏检查',比较对象的原始状态和当前状态。如果它们不同并且Hibernate认为这种差异会使你暴露于陈旧的数据,它将尝试将这个状态推送到数据库。这是可能的,因为Hibernate知道查询会触摸哪些表,它将需要更新当前脏状态的表。



看看这个 article


在什么时候刷新时自动刷新是非常聪明的,尽管
在某些情况下可能很难检测到。为了提高性能,
Hibernate不会简单地总是刷新所有内容,而是查看数据库中查询触及的
表和应该存储在这些表中的数据内存
。如果没有重叠,则不会持有数据
。如果发现重叠,整个会话将刷新
,以确保一致性。


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

FlushMode.AUTO




  • 有几个缺点,您无法控制何时Hibernate将决定执行UPDATE / INSERT / DELETE

  • 潜在的性能问题,因为每个对象修改都可能导致脏检查+执行DML语句

  • 不利用Hibernate在试图避免'过时'状态时可以执行的批处理和其他优化



由于这些缺点我更喜欢 FlushMode.COMMIT 这给你更多的控制。在这种模式下,Hibernate会一直等到你明确地调用Commit或Flush,并且在这之后只同步内存中的数据库状态。


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

解决方案

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.

这篇关于在FlushMode.AUTO后面检查什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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