PDA

View Full Version : Code for SessionScope implementation



nokiola
08-29-2006, 10:21 PM
Hi,

Following the SessionScope example from CastleProject's ActiveRecord and inspired by the OpenSessionInViewModule, I have coded a small extension to Spring.Data.Orm.NHibernate.

The new class is called SessionScope and is located Spring.Data.Orm.NHibernate.Support namespace. The SessionScope class purpose is to augment performance by caching the session, thus avoiding too much opens/flushes/closes. By embedding your code in "using SessionScope" you obtain a single session to do all your data access operation. NHibernate lazy loading available in OpenSessionInViewModule for web apps is now possible. This way you can escape the inconvenient NHibernate.LazyInitializationException received due to the fact that the session was closed before the lazy object(s) have been initialized from database.

The session is now more long lived than a transaction which is useful for unit testing.

The code for this class is attached to this post.

If someone finds it useful, please use it freely and hapilly; maybe someday will make it into Spring.Data.Orm.NHibernate official project.

Example usage:

using (new SessionScope())
{
IAccountServices accountServices = ctx["AccountServices"] as IAccountServices;
Account account = accountServices.GetAccount(20);
if (account != null)
{
account.Name = "Modified name";
accountServices.UpdateAccount(account);
}
}

Note: UpdateAccount and GetAccount are marked with [Transaction] attribute and demarcate two transactions in the same session.

Looking forward for your feedback.

Regards,
Robert

jnapier
08-31-2006, 06:31 PM
So how do you set this up for a web app? Will OpenSessionInViewModule just automatically work now? Can you still use HibernateTemplate to get the benefits from SessionScope?

nokiola
08-31-2006, 10:28 PM
I've attached two sample files which try to exemplify a possible usage scenario for SessionScope object.

In a web app I believe the OpenSessionInViewModule might be the recommended solution. However you can combine both OpenSessionInView functionality with SessionScope to achive more complex actions.

Please review "ThisIsAnExampleTest" method to see how you can use SessionScope class. HibernateTemplate works as expected and its usage is exemplified in the GenericDAO abstract class.

Regards,
Robert

Disclaimer: Please note that the cs file was put together in Notepad from many separate files for demo purposes and the code might not be compilable. :-)

jnapier
09-01-2006, 06:01 PM
The problem is that the OpenSessionInViewModule doesnt even work as expected right now. So your sessionScope would be handly for non web apps but doesnt help resolve any problems with a web application. Really what is needed is a solution that solves the problems for web and non web apps.

Erich Eichinger
09-01-2006, 11:11 PM
Hi,

In class OpenSessionInView in method context_BeginRequest() I changed the line


TransactionSynchronizationManager.BindResource(ses sionFactory, new SessionHolder(session));

to


TransactionSynchronizationManager.BindResource(ses sionFactory, new SessionHolder(session));
TransactionSynchronizationManager.InitSynchronizat ion();


I'm not sure, if this is correct usage of the TransactionSynchronizationManager. But it make's the Module work as expected. And one should be able to use the same code even for non-webapps.

cheers,
Erich

pathakn
10-18-2006, 06:54 AM
And ofcourse you also need to add following



TransactionSynchronizationManager.UnbindResource(s essionFactory);
TransactionSynchronizationManager.ClearSynchroniza tion();

in PerformDisposal.

Mark Pollack
10-18-2006, 03:43 PM
Hi,

I'm all for adding support for keeping the session scope open....I'd like to look into more the option available in Spring.Java of 'deferred close mode' in which case each transaction uses a new hibernate session and they are all kept open for the page rendering. In anycase, I'll grab the code and commit it so that other can use/review it. something of this form will definietly be in the release. Thanks for your patience...

Mark

Mark Pollack
10-18-2006, 04:16 PM
err, obviously I'll be reviewing it as well...made a typo.

Mark Pollack
11-21-2006, 05:43 AM
Hi,
Just an update regarding OpenSessionInView, I've fixed the bug that was preventing re-use of the session created in the http module so the quick-fix listed above should on longer be used. Apologies for the long turn around time.
- Mark