开发者

NHibernate: What are child sessions and why and when should I use them?

开发者 https://www.devze.com 2023-02-05 07:00 出处:网络
In the comments for the ayende\'s blog about the auditing in NHibernate there is a mention about the need to use a child session:session.GetSession(EntityMode.Poco).

In the comments for the ayende's blog about the auditing in NHibernate there is a mention about the need to use a child session:session.GetSession(EntityMode.Poco).

As far as I understand it, it has something to do with the order of the SQL operation which session.Flush will emit. (For example: If I wanted to perform some delete operation in the pre-insert event but the session was already done with deleting operations, I would need some way to inject them in.)

However I did not find documentation about this feature and behavior.

Questions:

  • Is my understanding of child sessions correct?

  • How and in which scenarios should I use them?

  • Are they documented somewhere?

  • Could they be used for session "scoping"?

    (For example: I open the master session which will hold some data and then I create 2 child-sessions from the master one. I'd expect that the two child-scopes will be separated but the w开发者_StackOverflow社区ill share objects from the master session cache. Is this the case?)

  • Are they first class citizens in NHibernate or are they just hack to support some edge-case scenarios?

Thanks in advance for any info.


Stefando,

NHibernate has not knowledge of child sessions, you can reuse an existing one or open a new one.

For instance, you will get an exception if you try to load the same entity into two different sessions.

The reason why it is mentioned in the blog, is because in preupdate and preinsert, you cannot load more objects in the session, you can change an allready loaded instance, but you may not navigate to a relationship property for instance.

So in the blog it is needed to open a new session just because we want to add a new auditlog entity. So in the end it's the transaction (unit of work) that manages the data.

0

精彩评论

暂无评论...
验证码 换一张
取 消