1

hi I'm designing my first asp.net web app, using nHibernate as the data provider.

i've read a lot about nhibernate in web applications using session-per-request pattern. my application will have a few pages which are 'monitors', meaning- they're updated automatically every few seconds to reflect recent changes to data. in that case, my thought is that opening a session for every request would not make much sense, since I know that an identical request is sure to follow in a few seconds.

my thought is that session-per-conversation would make more sense for me, but I'm having trouble finding examples of implementations. I'd appreciate any good resources for how to implement session-per-conversation, and any other ideas / suggestions you may have.

thanks

Jhonny

2 Answers 2

1

A session-per-request is still a good idea, as you don't know, in a web-scenerio, when a conversation ends. There's not much overhead to opening/closing a session, and the connection is only kept open whilst the session is in a transaction. , and, as a session wraps a database connection, you're simply using up connections from the connection pool by keeping your sessions open.

if you're stuck on a session-per-conversation stuff, look at NHibernate.Burrow - this handles all the conversation/session management stuff for you.

Sign up to request clarification or add additional context in comments.

3 Comments

The recommendation is good, but you have one point wrong: a session does not keep the connection open, except when inside a transaction.
I was also concerned, at first, about open db-connections, but as Diego pointed out- they're only open if a transaction is open. Part of my motivation for having an open session is to utilize its caching mechanism, as some objects are not likely to change often (for example- if I monitor stock in my store, the store departments and sections aren't likely to change, while stock levels obviously are). but then again, maybe I can utilize nhibernate's 2nd level cache.. any thoughts?
well, i've learned quite a bit about nHibernate and 2nd level cache since dec 11... :) indeed i'm using the session-per-request, as recommended, and 2nd level cache to hold the (relatively) static data (departments and sections). and it works very nicely. thanks!
0

If you were to use session-per-conversation, it seems to me that for the monitor pages the conversation would be ongoing during the entire user session. I would not recommend that approach because there are so many opportunities for problems in that scenario. I would recommend opening an IStatelessSession to update the monitor data display because that gives you the benefit of short-lived sessions without having the overhead of tracking object state.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.