Hibernate 何时以及如何刷新其会话?
When and how does Hibernate flush its session?
我不明白休眠 session.flush()
是如何工作的。在 grails 中,如果自动调用,什么时候调用它?它如何决定何时是刷新的最佳时间?如果用户执行了一些需要数千次查询的操作,在他的会话结束时 运行 怎么办?将其乘以千名用户。
您可以分享任何解释 grails 中休眠会话的内容吗?谢谢
Grails 遵循 "Open session in view" 处理 Hibernate 会话和自动刷新的模式。
在您的示例中,如果您没有在会话上显式调用 .flush()
或在修改数据时使用 flush: true
,它实际上会在线程启动时刷新用户会话的所有查询会话结束(通常在基于 Web 的 Grails 应用程序中的 HTTP 请求结束时)。
根据您的应用程序,定期刷新会话可能有意义。同样,这在很大程度上取决于您的应用程序以及单个用户请求影响的数据量。过于频繁地刷新也会降低性能。
如果您关心的是性能,那么只有一种真正的方法可以确定最佳方法。使用各种方法分析您的应用程序,看看哪种方法适合您的应用程序。
在 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.
我不明白休眠 session.flush()
是如何工作的。在 grails 中,如果自动调用,什么时候调用它?它如何决定何时是刷新的最佳时间?如果用户执行了一些需要数千次查询的操作,在他的会话结束时 运行 怎么办?将其乘以千名用户。
您可以分享任何解释 grails 中休眠会话的内容吗?谢谢
Grails 遵循 "Open session in view" 处理 Hibernate 会话和自动刷新的模式。
在您的示例中,如果您没有在会话上显式调用 .flush()
或在修改数据时使用 flush: true
,它实际上会在线程启动时刷新用户会话的所有查询会话结束(通常在基于 Web 的 Grails 应用程序中的 HTTP 请求结束时)。
根据您的应用程序,定期刷新会话可能有意义。同样,这在很大程度上取决于您的应用程序以及单个用户请求影响的数据量。过于频繁地刷新也会降低性能。
如果您关心的是性能,那么只有一种真正的方法可以确定最佳方法。使用各种方法分析您的应用程序,看看哪种方法适合您的应用程序。
在 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.