Hibernate Books

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: NHibernate session managment
PostPosted: Sat Jun 16, 2012 8:14 am 
Newbie

Joined: Sat Jun 16, 2012 8:10 am
Posts: 1
I use NHiberante at my windows service. Sometimes I get this error stack:

Code:
System.ObjectDisposedException: Session is closed!
Object name: 'ISession'.
   at NHibernate.Impl.AbstractSessionImpl.ErrorIfClosed()
   at NHibernate.Impl.AbstractSessionImpl.CheckAndUpdateSessionStatus()
   at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.Save(Object obj)
   at Attraction.DAL.Repositories.Repository`1.Save(T entity)
   at Attraction.VideoDispatcher.Program.ThreadPoolCallback(Object threadContext)


I have no idea what's wrong. My session management subsystem:

Repository:

Code:
public class Repository<T> : IRepository<T>, IDisposable
    {
        protected readonly bool CommitAtDispose;
        public Repository(bool commitAtDispose)
        {
            CommitAtDispose = commitAtDispose;
            StartSession();
        }
        private void StartSession()
        {
            if (NHibernateSession == null)
                NHibernateHelper.StartSession();
        }
        public void Dispose()
        {
            if (CommitAtDispose)
                Flush();
        }
        public void Flush()
        {
            NHibernateHelper.EndSession();
        }
        protected override sealed ISession NHibernateSession
        {
            get
            {
                return SessionManager.CurrentSession;
            }
        }       
        public virtual T GetById(int id)       
        public virtual List<T> GetAll()       
        public virtual List<T> GetByPage(int pageIndex, int pageSize)       
        public virtual int GetCount()       
        public virtual List<T> GetByCriteria(params ICriterion[] criterion)       
        public virtual T Save(T entity)       
        public virtual T Update(T entity)       
        public virtual void Delete(T entity)       
    }
}


SessionManager - singletone for provide access to sessionfactory
Code:
public class SessionManager : ISessionFactoryProvider
{
        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private readonly ISessionFactory sessionFactory;
        public static ISessionFactory SessionFactory
        {
            get { return Instance.sessionFactory; }
        }
        public ISessionFactory GetSessionFactory()
        {
            return sessionFactory;
        }
        public static ISession OpenSession()
        {
            return Instance.GetSessionFactory().OpenSession();
        }
        public static ISession CurrentSession
        {
            get
            {
                if (!CurrentSessionContext.HasBind(Instance.GetSessionFactory()))
                    return null;
                return Instance.GetSessionFactory().GetCurrentSession();
            }
        }


        public static SessionManager Instance
        {
            get
            {
                return NestedSessionManager.sessionManager;
            }
        }
        private SessionManager()
        {
            Log.Info("Start creating factory");
            Configuration configuration = new Configuration().Configure();
            sessionFactory = configuration.BuildSessionFactory();
            Log.Info("End creating factory");

        }

        class NestedSessionManager
        {
            internal static readonly SessionManager sessionManager =
                new SessionManager();
        }
    }


NhibernateHelper, which do some work for start and end session:

Code:
public static class NHibernateHelper
    {
        public static void StartSession()
        {
            var session = SessionManager.SessionFactory.OpenSession();
            session.BeginTransaction();
            CurrentSessionContext.Bind(session);

        }
        public static void EndSession()
        {
            var session = SessionManager.CurrentSession;
            CurrentSessionContext.Unbind(SessionManager.SessionFactory);
            if (session != null)
            {
                try
                {
                    if (session.Transaction != null && session.Transaction.IsActive)
                        session.Transaction.Commit();
                }
                catch (Exception ex)
                {
                    session.Transaction.Rollback();
                    throw new ApplicationException("Error committing database transaction. "+ex.Message, ex);
                }
                finally
                {
                    session.Close();
                    session.Dispose();

                }
            }


        }
    }


My config. I haven't migrate to fluent yet so:

Code:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
      <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
      <property name="connection.connection_string">***</property>
      <property name="show_sql">false</property>
      <property name="default_schema">**_***</property>
      <property name="current_session_context_class">thread_static</property>
      <mapping assembly="***.Core"/>
    </session-factory>
  </hibernate-configuration>


May be my design isn't so good, but I couldn't imagine how can I catch this error.


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

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.