tags:

views:

30

answers:

3

A silly question, perhaps, but at this time of night, StackOverFlow is my only friend.

I'm playing with NHibernate and wanted to factualize these 2 statements regarding Sessions in web applications.

1) You should only ever have 1 ISessionFactory per database for the lifecycle of an application.

2) You should only have 1 ISession per HttpRequest or batch of HttpRequests (i.e. conversation)

[I don't want tool or framework recommendation, just want to confirm the above]

+1  A: 

You should only have one ISessionFactory for the lifecycle of the application.

Session-per-request is how I work with NHibernate. There may be other patterns, but this is the one that I stick to in my web projects.

Remi Despres-Smyth
+1  A: 

You are correct in your assumptions.

Session-per-request is the most common pattern for web applications, and plays nice with MVC, web farms, etc.

There are some ready-to-use modules to handle these patterns at http://unhaddins.googlecode.com/svn/trunk/uNhAddIns/uNhAddIns.Web/ (you can check the rest of uNhAddIns too)

Diego Mijelshon
A: 

We currently utilize Session-per-Request however I have come across issues with this approach in some cases.

I don't think the answer is that generic, and you should consider using one session per unit of work as well.

Especially when it comes to automagic flushing of entities the Session per request might bite you in the ass.

jishi