gpt4 book ai didi

hibernate - Hibernate何时以及如何刷新其 session ?

转载 作者:行者123 更新时间:2023-12-02 13:55:57 25 4
gpt4 key购买 nike

我不了解休眠session.flush()的工作方式。在grails中,何时自动调用它;何时确定最佳冲洗时间?如果用户执行了一些需要数千个查询才能在其 session 结束时运行的操作,该怎么办?乘以成千上万的用户。

您可以分享的任何信息可以解释grails的休眠状态吗?谢谢

最佳答案

Grails遵循处理休眠 session 和自动刷新的“在 View 中打开 session ”模式。

在您的示例中,如果您没有在 session 上显式调用.flush()或在修改数据时使用flush: true,则实际上,当启动 session 的线程结束时(通常在HTTP请求的结尾),将刷新用户 session 的所有查询。基于Web的Grails应用程序)。

根据您的应用程序,定期刷新 session 可能很有意义。同样,这在很大程度上取决于您的应用程序以及单个用户请求会影响多少数据。过于频繁的冲洗也会破坏性能。

如果您关注性能,则只有一种方法可以确定最佳方法。使用各种方法来分析您的应用程序,然后查看哪种方法适合您的应用程序。

Grails documentation中找到有关此主题的一些其他信息:

A useful feature of Hibernate over direct JDBC calls and even other frameworks is that when you call save or delete it does not necessarily perform any SQL operations at that point. Hibernate batches up SQL statements and executes them as late as possible, often at the end of the request when flushing and closing the session. This is typically done for you automatically by Grails, which manages your Hibernate session.

Hibernate caches database updates where possible, only actually pushing the changes when it knows that a flush is required, or when a flush is triggered programmatically. One common case where Hibernate will flush cached updates is when performing queries since the cached information might be included in the query results. But as long as you're doing non-conflicting saves, updates, and deletes, they'll be batched until the session is flushed. This can be a significant performance boost for applications that do a lot of database writes.

Note that flushing is not the same as committing a transaction. If your actions are performed in the context of a transaction, flushing will execute SQL updates but the database will save the changes in its transaction queue and only finalize the updates when the transaction commits.

关于hibernate - Hibernate何时以及如何刷新其 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28383866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com