These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: CurrentSessionContext problem in ASP.NET
PostPosted: Tue Oct 16, 2007 10:08 am 
Regular
Regular

Joined: Tue Oct 16, 2007 9:45 am
Posts: 93
Hi, I have a problem with SessionFactory.GetSession. It throws an error: "No CurrentSessionContext configured (set the property hibernate.current_session_context_class)!"

Hibernate version: 1.2

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- an ISessionFactory instance -->
<session-factory xmlns='urn:nhibernate-configuration-2.2'>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2000Dialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string">
Initial Catalog=Northwind;Integrated Security=SSPI
</property>
<property name="hibernate.current_session_context_class">
web
</property>
</session-factory>
</configuration>

HttpModule
public class NHibernateSessionModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
context.EndRequest += new EventHandler(Application_EndRequest);
}

public void Dispose()
{
}

private void Application_BeginRequest(object sender, EventArgs e)
{
///??? not sure if this is needed
if (ManagedWebSessionContext.HasBind(HttpContext.Current, NHibernateHelper.SessionFactory))
{
ManagedWebSessionContext.Unbind(HttpContext.Current, NHibernateHelper.SessionFactory);
}

ManagedWebSessionContext.Bind(HttpContext.Current,
NHibernateHelper.SessionFactory.GetCurrentSession());
}

private void Application_EndRequest(object sender, EventArgs e)
{
NHibernate.ISession session = ManagedWebSessionContext.Unbind(HttpContext.Current,
NHibernateHelper.SessionFactory);

if (session.Transaction.IsActive)
{
session.Transaction.Rollback();
}

if (session != null)
{
session.Close();
}
}
}

NHibernateHelper

public class NHibernateHelper
{
public static readonly ISessionFactory SessionFactory;

static NHibernateHelper()
{
try
{
Configuration cfg = new Configuration();
cfg.Configure(HostingEnvironment.MapPath(
System.Configuration.ConfigurationManager.AppSettings["NHibernateConfigurationFile"]));
cfg.AddXmlFile(HostingEnvironment.MapPath("~/bin/Category.hbm.xml"));
SessionFactory = cfg.BuildSessionFactory();
}
catch (Exception ex)
{
throw new Exception("NHibernate initialization failed", ex);
}
}

public static ISession GetCurrentSession()
{
return SessionFactory.GetCurrentSession();
}


Error occurs in Application_BeginRequest when executing
ManagedWebSessionContext.Bind(HttpContext.Current,
NHibernateHelper.SessionFactory.GetCurrentSession());

Whatt am I missing here?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 18, 2007 8:21 am 
Regular
Regular

Joined: Tue Oct 16, 2007 9:45 am
Posts: 93
Nobody has any ideas what to look for?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 3:40 pm 
Newbie

Joined: Tue Dec 11, 2007 3:33 pm
Posts: 2
Hi

I had the same problem and i solved it by using

<property name="hibernate.current_session_context_class">
NHibernate.Context.ThreadStaticSessionContext, NHibernate
</property>

instead of

<property name="hibernate.current_session_context_class">
web
</property>

:)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 4:53 pm 
Beginner
Beginner

Joined: Mon Oct 22, 2007 5:44 am
Posts: 28
Hi,

It seems that Nhibernate does not reconized the web value so
try to use managed_web instead of web.

_________________
dyahav


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 13, 2007 1:52 am 
Newbie

Joined: Tue Dec 11, 2007 3:33 pm
Posts: 2
Oops!!

I did a mistake in my last posting, i mean that i used

<property name="hibernate.current_session_context_class">
NHibernate.Context.WebSessionContext, NHibernate
</property>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 9:51 am 
Regular
Regular

Joined: Tue Oct 16, 2007 9:45 am
Posts: 93
I got this from "NHibernate in Action" book. This is what is says in book
    NHibernate’s built-in current session context implementations
    Short name Description
    *managed_web This context was the only one available in NHibernate 1.2.0. However, it is now
    deprecated. You should use web instead.
    *call This context uses the CallContext API to store the current session. Note that,
    althought it works in any kind of application, it isn’t recommended for ASP.NET
    2.0 applications.
    *thread_static When using this context, sessions are stored in a static field marked with
    [ThreadStaticAttribute]. Therefore, each thread has his own session.
    * web This context uses the HttpContext API to store the current session. It is
    recommended for Web applications (and only works with them).
[/quote]

Now I've been away from NH for a while, and I wonder if anything has changed. Is this now in 1.2.1 release?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.