views:

116

answers:

2

In the company I work for, we're using a NHibernate session wrapper that disposes all the sessions opened in the current web request at the end of the same request, and commits all the associated transactions (we're working in a multi-database environment, and we create a session for every database).

Also, in the session wrapper we're using, the session and the transactions are coupled, and we can't commit a transaction without disposing its session.

The problem is that, sometimes, a transaction stays open for all the request life, and database tables stays locked for much too time.

... are we missing something?

+2  A: 

Session per request is only one way to do session lifecycle management. It happens to be the recommended strategy for web apps, but NHibernate is really indifferent to how you manage your session lifetime.

You can certainly use sessions that have shorter or longer lives than a single request. If you have use cases that would work better in that way, by all means, go for it.

Don't let your session wrapper put unnecessary constraints on your ability to adapt NHibernate to the needs of your application.

Michael Maddox
Are there limitations to use multiple sessions per requests? For example, the inhability to access a lazy property? Otherwise, if we use multiple transactions inside a single request, will the lazy properties of an entity shared by multiple transactions continue to work fine?
Notoriousxl
@Notoriousxl: Lazy properties can only be accessed within the same session as the object was retrieved in. As far as what will work and what won't just write some simple unit/integration tests covering the scenarios you are concerned about to prove they will work as you need them to work.
Michael Maddox
+2  A: 

Sound more like a limitation of the session manager you're using. Granted all open transactions should be disposed at the end of the session (and rolled back if not committed) - you should be able to create and commit transactions as needed. It really comes down to how you want to handle the service call - if you want it treat the service call as one atomic transaction I think you are stuck with locking until completion. Lot's of options here.

Christopherous 5000
+1 Session-per-request is fine, but the programmer should have control over when the session is opened and closed to isolate the data operations as tightly as possible. If there's an issue with the time of a request being way longer than the time of a typical data query, that is absolutely the first thing to fix.
Jon Seigel