指定 hibernate.jdbc.batch_size 有什么意义?

发布于 2024-12-28 02:59:43 字数 333 浏览 1 评论 0原文

这个 Hibernate 配置表面上应该控制在一级缓存中缓存多少对象。原因很容易理解,我们不想耗尽内存。

但有些事情让我困惑。我见过的每个实现包括 这个网站< /a> 具有明确的齐平和清晰。没问题,但是配置属性有什么意义呢?

注意:我假设 Hibernate 以某种方式监视缓存的大小,如果某种类型的对象数量增长到大于缓存大小,则将缓存与数据库同步。不知道这个假设是否错误???

This Hibernate configuration ostensibly should control how many objects are cached in the first level cache. The reason is easy enough to understand, we don't want to run out of memory.

But something is confusing me. Every implementation I have seen including this website
has an explicit flush and clear. No problem, but then what's point of the configuration property?

Note: I'm assuming here is that somehow Hibernate monitors the size of the cache and if the number of objects of a certain type grows to be larger than the cache size then synchronize the cache with the db. Don't know if that assumption is wrong ???

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

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

发布评论

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

评论(1

心碎无痕… 2025-01-04 02:59:43

该配置选项与一级缓存的大小无关。刷新会话不会从缓存中删除任何内容。它将挂起的更改(插入、删除、更新)写入数据库。仅当显式调用clear() 或会话关闭时才会清除缓存。如果您不清除或子句会话(或退出特定实体),缓存将不断增长。这不是问题,因为它通常非常短暂(交易的持续时间)。

JDBC 批量更新允许在单个批次中向数据库发送多个更新查询。它减少了网络调用的数量。您可以将其视为上传包含 20 个文件的未压缩 zip,而不是单独发送 20 个文件。

造成混乱的原因是您问题中链接的页面中提到的批量更新与 JDBC 批量更新无关。 Hibernate 对于批量更新的含义是“通过批处理作业完成的更新”。批处理作业通常具有比典型业务用例更长的事务,并在单个事务中更新数百、数千甚至更多实体。这就是为什么 Hibernate 建议在这种情况下定期刷新和清除会话,以避免内存不足。

This configuration option has nothing to do with the size of the first-level cache. And flushing the session doesn't remove anything from the cache. It writes the pending changes (inserts, deletes, updates) to the database. The cache is only cleared when clear() is explicitely called, or when the session is closed. If you don't clear or clause the session (or evist specific entities), the cache will keep growing and growing. Which is not a problem since it's typically very short-lived (the duration of a transaction).

JDBC batch updates allow sending multiple update queries in a single batch to the database. It reduces the number of network calls. You can view it as uploading an uncompressed zip containing 20 files instead of sending 20 files individually.

The confusion comes from the fact that the batch updates mentioned in the page linked in your question have nothing to do with JDBC batch updates. What Hibernate means with batch updates is "updates done by a batch job". A batch job typically have much longer transactions that typical business use-cases, and updates hundreds, thousands or even more entities in a single transaction. This is why Hibernate recommends regularly flushing and clearing the session in this case, to avoid running out of memory.

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