NHibernate的ISession的冲洗:在哪里以及何时使用它,为什么? [英] NHibernate ISession Flush: Where and when to use it, and why?

查看:401
本文介绍了NHibernate的ISession的冲洗:在哪里以及何时使用它,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个是让我彻底糊涂的地方是使用 session.Flush 的,连同 session.Commit session.Close

One of the things that get me thoroughly confused is the use of session.Flush,in conjunction with session.Commit, and session.Close.

有时候 session.Close 的作品,例如,它要求所有我需要的变化。我知道我需要使用提交时我有一个交易,或工作的几个单元创建/更新/删除,这样我可以选择,如果发生错误回滚。

Sometimes session.Close works, e.g., it commits all the changes that I need. I know I need to use commit when I have a transaction, or a unit of work with several creates/updates/deletes, so that I can choose to rollback if an error occurs.

但有时我真的得到由背后 session.Flush 逻辑阻碍。我所看到的,你有例子 session.SaveOrUpdate()接着冲洗,但是当我删除冲洗它工作正常呢。有时候我碰到的同花顺声明错误说,会话超时,并删除它确信,我并没有遇到这个错误。

But sometimes I really get stymied by the logic behind session.Flush. I have seen examples where you have a session.SaveOrUpdate() followed by a flush, but when I remove Flush it works fine anyway. Sometimes I run into errors on the Flush statement saying that the session timed out, and removing it made sure that I didn't run into that error.

有没有人有一个很好的指引在何处,或何时使用冲洗?我已经签出了NHibernate的文档,但我仍然无法找到一个明确的答案。

Does anyone have a good guideline as to where or when to use a Flush? I've checked out the NHibernate documentation for this, but I still can't find a straightforward answer.

推荐答案

简而言之:

  1. 始终使用事务
  2. 请不要使用关闭(),而不是包裹你的电话上的ISession A <$ C $内C>使用语句或管理您的ISession的生命周期别处
  1. Always use transactions
  2. Don't use Close(), instead wrap your calls on an ISession inside a using statement or manage the lifecycle of your ISession somewhere else.

文档

不时的的ISession 将执行与在内存中对象的状态同步ADO.NET连接的状态所需的SQL语句。这个过程中,同花顺,默认会在以下几点

From time to time the ISession will execute the SQL statements needed to synchronize the ADO.NET connection's state with the state of objects held in memory. This process, flush, occurs by default at the following points

      
  • 部分调用查找()可枚举()
  •   
  • NHibernate.ITransaction.Commit()
  •   
  • ISession.Flush()
  •   
  • from some invocations of Find() or Enumerable()
  • from NHibernate.ITransaction.Commit()
  • from ISession.Flush()

在SQL语句按以下顺序发出

The SQL statements are issued in the following order

      
  1. 在所有实体插入,以相同的顺序相应的对象执行保存 ISession.Save()
  2.   
  3. 在所有的实体更新
  4.   
  5. 在所有进行集合删除
  6.   
  7. 在所有集合元素进行删除,更新和插入
  8.   
  9. 在所有集合插入
  10.   
  11. 在所有实体删除,以相同的顺序相应的对象执行删除 ISession.Delete()
  12.   
  1. all entity insertions, in the same order the corresponding objects were saved using ISession.Save()
  2. all entity updates
  3. all collection deletions
  4. all collection element deletions, updates and insertions
  5. all collection insertions
  6. all entity deletions, in the same order the corresponding objects were deleted using ISession.Delete()

(一个例外是,使用本机ID生成对象,它们被保存时被插入。)

(An exception is that objects using native ID generation are inserted when they are saved.)

除非你明确地同花顺(),绝对没有关于当届执行ADO.NET电话,只有这些是为了保证执行。当然,NHibernate并保证 ISession.Find(..)方法将不会返回已经失效的数据;他们也不会返回错误数据。

Except when you explicity Flush(), there are absolutely no guarantees about when the Session executes the ADO.NET calls, only the order in which they are executed. However, NHibernate does guarantee that the ISession.Find(..) methods will never return stale data; nor will they return the wrong data.

这是可能的,这样刷新后不经常更改默认行为。该 FlushMode 类定义了三种不同的模式:仅在提交时刷(只有当NHibernate的 ITransaction API时) ,采用刚才说的自动冲洗,或从不除非刷新刷新()显式调用。对于长时间运行的工作,凡的ISession 保持开放和断开时间长。

It is possible to change the default behavior so that flush occurs less frequently. The FlushMode class defines three different modes: only flush at commit time (and only when the NHibernate ITransaction API is used), flush automatically using the explained routine, or never flush unless Flush() is called explicitly. The last mode is useful for long running units of work, where an ISession is kept open and disconnected for a long time.

...

另请参阅本节

结束会话包括四个不同的阶段:

Ending a session involves four distinct phases:

      
  • 刷新会议
  •   
  • 提交事务
  •   
  • 关闭会话
  •   
  • 处理异常
  •   
  • flush the session
  • commit the transaction
  • close the session
  • handle exceptions

如果你碰巧使用了 ITransaction API,你不需要担心这个步骤。这将是隐式执行时提交事务。否则,你应该叫 ISession.Flush(),以确保所有的改变都与数据库同步。

If you happen to be using the ITransaction API, you don't need to worry about this step. It will be performed implicitly when the transaction is committed. Otherwise you should call ISession.Flush() to ensure that all changes are synchronized with the database.

如果您正在使用NHibernate的ITransaction API,这看起来像:

If you are using the NHibernate ITransaction API, this looks like:

tx.Commit(); // flush the session and commit the transaction

如果您在管理ADO.NET交易自己,你应该手动提交()的ADO.NET事务。

If you are managing ADO.NET transactions yourself you should manually Commit() the ADO.NET transaction.

sess.Flush();
currentTransaction.Commit();

如果您决定不提交更改:

If you decide not to commit your changes:

tx.Rollback();  // rollback the transaction

     

currentTransaction.Rollback();

如果你回滚事务你应该立即关闭并丢弃在本届会议,以确保NHibernate的内部状态是一致的。

If you rollback the transaction you should immediately close and discard the current session to ensure that NHibernate's internal state is consistent.

一个调用 ISession.Close()标志着一个会话结束。的关闭()的主要含义是ADO.NET连接将被会话被放弃。

A call to ISession.Close() marks the end of a session. The main implication of Close() is that the ADO.NET connection will be relinquished by the session.

tx.Commit();
sess.Close();

sess.Flush();
currentTransaction.Commit();
sess.Close();

如果您提供自己的连接,关闭()返回对它的引用,所以你可以手动关闭,或将其返回到池中。否则关闭()返回给池。

If you provided your own connection, Close() returns a reference to it, so you can manually close it or return it to the pool. Otherwise Close() returns it to the pool.

这篇关于NHibernate的ISession的冲洗:在哪里以及何时使用它,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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