开发者

NHibernate - Session Singleton C#

Trying to migrate NHibernate into an existing C# WinForms app and I am currently looking for the best way to manage sessions. I am only learning NHibernate 开发者_StackOverflow中文版so please excuse my lack of knowledge, can someone please provide me with any Session management code which we can use, most of the implementations that we have found are for web apps.

Any help is appreciated.


Assuming you're already comfortable with XML and the NHibernate configuration itself, there is one object that you need to make a singleton, that is, the ISessionFactory API. The reason why this should be made a singleton is that the full configuration in regards to your connection, plus all of the entity mappings are loaded in memory so that NHibernate know how and where to persist into the underlying datastore. Hence, the ISessionFactory API is very costly to instantiate.

As for the sessions, there are two aspects to consider, whether you want a stateless or a statefull session. This choice is yours entirely, but know the difference.

The ISession API keeps track of every single change on an attached entity. An attached entity is one that has either been loaded from the underlying datastore using one of the possible ways provided by NHibernate, or, on the other hand, an transient entity that has just been instantiated and that you have attached yourself to the session. Keeping such a statefull session alive for toolong may cause data loss as will come the time where this API leaks in memory. Once a leak occurs, the entire track of changes is trashed and the session can no longer be used. The ISession API shall always be used along with the ITransaction API. The ITransaction is likely the same as a Begin Transaction directly in SQL (TSQL, PL/SQL and others). Everytime you need to persist into the database, you should do so while using a transaction in order to prevent errors from voiding the changes or whatsoever. The ISession API should be instantiated in the beginning of a unit-of-work. One only ISession API instance might be required to handle more than one form at a time. That said, making an ISession API instance a singleton is a really, really bad idea, for the memory management reason, for which the ISession keeps track of every changes occuring to an attached entity. Keeping the ISession API alive for too long will cause it to use more and more memory, which will result in memory leaks, and trash of the session.

The IStatelessSession API does what it says to be. Being stateless, it doesn't keep track of any changes made to an attached object. So you will have to manually know what is to be done as an action against the database for this entity you intend to persist, let's say. The IStatelessSession also uses the ITransaction API through which it can handle data integrity. These both should always be used along each other while connecting to the datastore. The IStatelessSession is often used under the create-when-needed basis, as it has no other use than to "talk" with the datastore. This API is mostly used, I believe, in Web application where stateless rules the most.

Once you have chosen what best meets your requirements between the ISession and the IStatelessSesion APIs, you're ready to go.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜