Hello,

I am trying to work with managed sessions in my web application (MVC4). I 
have read tutorials but I feel I am doing something wrong. I don't know why 
but I feel that I get the same session for many request because I often get 
"Open data reader..." exception. The problem is that I get this exception 
even if I am sure that I have not used any command, especially in 
BeginTransaction().... Commit() block. There is some example function below:
 
 

> ISession session = NHibernateSessionManager.CurrentSession;
>
> using (ITransaction tx = session.BeginTransaction())
> {
>                 var account = session.Query<....................>
>
>                 try
>                 {
>                     var q = session.Load<..........................>
>
                    q.someProperty = 'a'; 
>
                    session.Update(q);                            
>                        
>                     tx.Commit();
>                 }
>                 catch (Exception e)
>                 {
>                     tx.Rollback();
>                 }
>  }  
>


My nhibernate web.config configuration file:

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
>     <session-factory>
>       <property 
> name="current_session_context_class">NHibernate.Context.ManagedWebSessionContext,
>  
> NHibernate</property>
>       <property 
> name="dialect">NHibernate.Dialect.PostgreSQL82Dialect</property>
>       <property 
> name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
>       <property name="connection.connection_string">...</property>
>       <property 
> name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory,
>  
> NHibernate</property>
>       <property name="show_sql">true</property>
>       ...
>     </session-factory>
>   </hibernate-configuration>
>

As you can see, I have set ManagedWebSessionContext to get one session per 
request. I assume that I that it will be new session per each every 
request, by using this code in global.asax.cs:

        protected void Application_BeginRequest(object sender, EventArgs e)
>         {
>             ManagedWebSessionContext.Bind(
>                 HttpContext.Current,
>                 NHibernateSessionManager.SessionFactory.OpenSession());
>         }
>
>         protected void Application_EndRequest(object sender, EventArgs e)
>         {
>             ISession session = ManagedWebSessionContext.Unbind(
>                 HttpContext.Current, 
> NHibernateSessionManager.SessionFactory);
>
>             if (session != null)
>             {
>                 if (session.Transaction != null && 
> session.Transaction.IsActive)
>                     session.Transaction.Rollback();
>
>                 session.Close();
>             }
>         }         
>

It uses NHibernateSessionManager class listed below to manage sessions:

    public class NHibernateSessionManager
>     {
>         private static object SyncObject = new object();
>         private static ISessionFactory _SessionFactory = null;
>
>         internal static ISessionFactory SessionFactory
>         {
>             get
>             {
>                 lock (SyncObject)
>                 {
>                     if (_SessionFactory == null)
>                     {
>                         Configuration configuration = new 
> Configuration().Configure();
>                         _SessionFactory = 
> configuration.BuildSessionFactory();
>                     }
>
>                     return _SessionFactory;
>                 }
>             }
>         }
>
>         public static ISession OpenSession()
>         {
>             lock (SyncObject)
>             {
>                 return SessionFactory.OpenSession();
>             }
>         }
>
>         public static ISession CurrentSession
>         {
>             get
>             {
>                 return SessionFactory.GetCurrentSession();
>             }
>         }
>     }
>

Any help would be appreciated. If you have some tested and working code, 
such help would be great as well.

Best regards.





-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to