什么应该是一个NHibernate会话的生命周期? [英] What should be the lifetime of an NHibernate session?

查看:237
本文介绍了什么应该是一个NHibernate会话的生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的NHibernate,和闭幕会议prematurely的时候已经看到了一些问题。我已经通过重用会议,而不是每次交易的打开会暂时解决了这一点。但是,我是在IM pression每次需要它们对于会话生命周期管理的建议方法时间打开会话。没有?

I'm new to NHibernate, and have seen some issues when closing sessions prematurely. I've solved this temporarily by reusing sessions instead of opening a session per transaction. However, I was under the impression that opening sessions each time you need them was the recommended approach for session lifetime management. No?

所以,什么是处理会议推荐的方法是什么?我应该有生之年是什么?其中一次会议的公关事务?一个单身会议,以处理一切吗?或者是什么?

So; what is the recommended way of handling sessions? What should their lifetime be? One session pr transaction? One singleton session to handle everything? Or what?

编辑:

请注意,我的应用程序体系结构是一个桌面应用程序与服务器端的服务,这是什么做的所有数据库的处理,使用NHibernate +流利的沟通。 (如果这使得任何区别...)

Note that my application architecture is a desktop application communicating with a server side service, which is what does all the Database handling, using NHibernate + Fluent. (If this makes any difference...)

推荐答案

您需要一个会话管理策略,可以让你的应用程序,以有效地发挥作用,走的东西,NHibernate的给你的优势 - 最明显的是缓存和延迟加载。

You want a session management strategy that allows your app to function effectively and take advantage of the things that NHibernate gives you - most notably caching and lazy loading.

创建会话是一种廉价的过程,需要很少的前期RAM或CPU,所以你不应该担心保存或再使用会话(事实上,重新使用它们可能会导致一些讨厌的和未预期的副作用效果)。该会话工厂是昂贵的东西,应该在应用程序启动一次,并且只有一次建成。

Creating sessions is an inexpensive process and requires little up-front RAM or CPU, so you shouldn't worry about conserving or re-using sessions (indeed, re-using them can lead to some nasty and un-anticipated side-effects). The session factory is the expensive thing and should be built once and only once at app startup.

经验法则是:在会话有效期必须足够长,你没有持久对象在会话结束后,在范围上游逛。

The rule of thumb is this: the session lifetime needs to be long enough that you don't have persisted objects hanging around in scope after the session ends.

在会话结束,所有的变化跟踪您从会议上得到了对象停止,所以这些变化没有得到保存,除非你故意的对象重新连接到一个新的会话。因此,会议应约,只要你从获取它会存在的对象。在Web应用程序中,这通常意味着会为每个请求;在的WinForms,会话为每个表单。

Once the session ends, all change tracking for objects you got from that session stops, so those changes don't get saved unless you deliberately re-attach that object to a new session. The session should therefore be around for as long as the objects you fetch from it are going to exist. In a Web app, that generally means a session for each request; in WinForms, a session for each form.

在你的情况,他们的服务(我假定它运行的 Windows服务)做NHibernate的工作,你不妨考虑让来自消费桌面应用程序的每个新的请求创建一个会话,和处置它时,该请求已被提供服务。不知道究竟是如何为您服务的运行,什么机制在桌面应用程序使用去跟它(远程处理?WCF?普通老式SOAP?)我真的不能更具体。

In your case, with a service (I assume it's running as a Windows service) doing the NHibernate work, you might wish to consider having a session created for each new request from the consuming desktop app, and disposing it when that request has been serviced. Not knowing exactly how your service runs and what mechanism the desktop app uses to talk to it (remoting? WCF? Plain old SOAP?) I can't really be more specific.

(有一些例外的一般规则 - 假设你有一组重present共享资源到其他code会参考但不会改变坚持的对象,就可以加载这些前期在应用程序启动并让他们断开从那时起。)

(There are some exceptions to this general rule - suppose you have a set of persisted objects that represent a shared resource to which other code will refer but not change, you can load these up-front at app-start and leave them disconnected from then on.)

如果你发现下了这样的策略表现低迷,这可能是因为你只是谈话的数据库太多,你的对象图是复杂的;看二级缓存在这种情况下。

If you find performance sluggish under such a strategy, it may be that you're just talking to the database too much and your object graph is complex; look at second-level caching in this case.

这篇关于什么应该是一个NHibernate会话的生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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