I am currently using NHibernate with Spring.Net and using Spring's Open Session In View module. As far as I can tell, this module only opens the session on BeginRequest and closes it on EndRequest. It does not actually commit or flush the session on EndRequest.
I know that you can use Spring's [Transaction] attribute to decorate individual service methods which will perform a commit at the end of the method, but I do not want to use this technique.
Is there any way to setup Spring's OSIV module so that it will flush the session on EndRequest? If not, is there an easy way to implement my own Open Session In View which would achieve this?
I have tried to implement the "session per web request" example from the NH 3 Cookbook, but it throws an error on CurrentSessionContext:
protected void Application_BeginRequest(object sender, EventArgs e)
{
var sessionFactory = (ISessionFactory) ContextRegistry.GetContext().GetObject("MySessionFactory");
var session = sessionFactory.OpenSession();
CurrentSessionContext.Bind(session);
}
protected void Application_EndRequest(object sender, EventArgs e)
{
var sessionFactory = (ISessionFactory)ContextRegistry.GetContext().GetObject("MySessionFactory");
var session = CurrentSessionContext.Unbind(sessionFactory);
session.Dispose();
}
Note: The above code only tries to replicate what Spring.Net is currently doing. I was planning on updating this to flush the session after I got it working.
I'm assuming that the above code is not working because I am using Spring.Net to setup NH and its SessionFactory which may make the example in the book invalid.
Any help/advice would be greatly appreciated.
I am using NHibernate 3.2 and Spring 1.3.2
Edit:
After reading http://forum.springsource.org/archive/index.php/t-16949.html I'm beginning to wonder if a transaction per request is a good idea.