简体   繁体   English

NHibernate FlushMode Never问题

[英]Problems with NHibernate FlushMode Never

We're building a large application with NHibernate as ORM layer. 我们正在使用NHibernate作为ORM层来构建大型应用程序。 We've tried to apply as many best practices as possible, among which setting FlushMode to Never . 我们尝试了尽可能多的最佳实践,其中将FlushMode设置为Never However, this is giving us pain, for example the following scenario: 但是,这使我们感到痛苦,例如以下情形:

There is a table with an end date column. 有一个带有结束日期列的表。 From this table, we delete the last (by end date) record: 在此表中,我们删除最后一个记录(按结束日期):

  • The record is deleted; 记录被删除;
  • After the delete, we do a (repository) query for the last record (by end date); 删除之后,我们(存储库)查询最后一条记录(按结束日期);
  • This last record is updated because it's the new active record. 最后一条记录已更新,因为它是新的活动记录。

This is a very simple scenario, of which many exist. 这是一个非常简单的场景,其中有许多场景。 The problem here is that when we do the query, we get the deleted record back, which of course is not correct. 这里的问题是,当我们执行查询时,我们会找回删除的记录,这当然是不正确的。 This roughly means that we cannot do queries in business logic that may touch the entity being inserted or deleted, because its resp. 这大致意味着,由于其响应,我们无法在业务逻辑中进行可能接触到要插入或删除的实体的查询。 not there yet or still there. 还不在那里或仍然在那里。

How can I work with this scenario? 我如何处理这种情况? Are there ways to work around this without reverting the FlushMode setting or should I just give up on the FlushMode setting all together? 有什么方法可以解决此问题而无需恢复FlushMode设置,还是我应该完全放弃FlushMode设置?

How can I work with this scenario? 我如何处理这种情况? Are there ways to work around this without reverting the FlushMode setting 有没有办法解决此问题而无需恢复FlushMode设置

FlushMode.Never does not prevent you from manually calling Flush() when you want to deal with up-to-date data. FlushMode.Never不会阻止您要处理最新数据时手动调用Flush() I guess it is the way to work this scenario without changing the FlushMode 我想这是在不更改FlushMode情况下解决此问题的方法

or should I just give up on the FlushMode setting all together? 还是我应该一起放弃FlushMode设置?

Could you provide some reference on FlushMode.Never being a good practice in the general case ? 您能否提供有关FlushMode.Never参考信息?在一般情况下,这不是一种好的做法吗? Seems like FlushMode.Never is fit when dealing with large, mostly readonly, sets of objects. 看起来像FlushMode.Never适合处理大型的,大多数为只读的对象集。

http://jroller.com/tfenne/entry/hibernate_understand_flushmode_never http://jroller.com/tfenne/entry/hibernate_understand_flushmode_never

FlushMode.Never is a best practice only when you absolutely require fine-grained control. 只有当您绝对需要细粒度的控制时, FlushMode.Never才是最佳实践。 FlushMode.Auto will cover 99.99% of the cases without a problem. FlushMode.Auto将覆盖99.99%的情况,而不会出现问题。 That said, decorating you CUD operations with a ISession.FLush() will not hurt as it only involves a database roundtrip if there are any CUD actions in the internal action queue 就是说,使用ISession.FLush()装饰CUD操作不会有什么坏处,因为如果内部操作队列中有任何CUD操作,则只涉及数据库往返

Flush mode Never means NHibernate will never flush the session, it's up to you to do that. 刷新模式Never意味着NHibernate永远不会刷新会话,这取决于您。 So, session.Delete() will not actually delete the record from database, just mark the object for delete in session's cache. 因此, session.Delete()实际上不会从数据库中删除记录,只是将对象标记为要在会话的缓存中删除。 You can force a flush by calling session.Flush() after calling session.Delete() . 您可以强制通过调用刷新session.Flush()调用后session.Delete()

I think Auto is a better option, with Auto , NHibernate will flush the session automatically before querying for data. 我认为Auto是更好的选择,有了Auto ,NHibernate将在查询数据之前自动刷新会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM