我抱怨我的应用中定期出现的问题。我的应用程序使用WPF,WCF,Fluent NHibernate和SQL。 自从我们将应用程序移至PROD环境(有30个活跃用户)后,问题就出现了;但是在UAT环境中(有5个活跃用户)app工作正常。
记录的异常:
System.ObjectDisposedException: Session is closed!
Object name: 'ISession'.
at NHibernate.Impl.AbstractSessionImpl.ErrorIfClosed()
at NHibernate.Impl.AbstractSessionImpl.CheckAndUpdateSessionStatus()
at NHibernate.Impl.SessionImpl.get_PersistenceContext()
at NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity, Boolean readOnly, ISessionImplementor session, PreLoadEvent preLoadEvent, PostLoadEvent postLoadEvent)
at NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session, Boolean readOnly)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, String optionalEntityName, Object optionalIdentifier, IEntityPersister persister)
我想与您分享NHibernateHelper课程,以检查它是否有任何错误:
namespace APCC.Business
{
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISession currentSession;
public static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
InitializeSessionFactory();
return _sessionFactory;
}
}
private static void InitializeSessionFactory()
{
//try
//{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(ConfigurationManager.ConnectionStrings["APCCDatabase"].ConnectionString)
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<BusinessUnitMap>()
.Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never()))
.ExposeConfiguration(cfg => new SchemaExport(cfg))//cfg => cfg.SetInterceptor(new AuditInterceptor()))//cfg => new SchemaExport(cfg))
.BuildSessionFactory();
//}
//catch (FluentConfigurationException ex)
//{
// ExceptionHelper.HandleException(ex);
//}
//catch (Exception ex)
//{
// ExceptionHelper.HandleException(ex);
//}
// .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
}
public static ISession OpenSession()
{
if(currentSession == null)
currentSession =SessionFactory.OpenSession();
return currentSession;
}
/// <summary>
/// Gets the current session.
/// </summary>
public static ISession GetCurrentSession()
{
if (currentSession != null)
return currentSession;
else
{
if (_sessionFactory == null)
{
InitializeSessionFactory();
currentSession = _sessionFactory.OpenSession();
}
else
{ currentSession = _sessionFactory.OpenSession(); }
//if (CurrentSessionContext.HasBind(_sessionFactory))
// return _sessionFactory.GetCurrentSession();
//CurrentSessionContext.Bind(currentSession);
return currentSession;
}
}
/// <summary>
/// Closes the session.
/// </summary>
public static void CloseSession()
{
if (currentSession!=null)
{
// var session = CurrentSessionContext.Unbind(_sessionFactory);
currentSession.Close();
currentSession = null;
}
}
///// <summary>
///// Commits the session.
///// </summary>
///// <param name="session">The session.</param>
//public static void CommitSession(ISession session)
//{
// try
// {
// session.Transaction.Commit();
// }
// catch (Exception)
// {
// session.Transaction.Rollback();
// throw;
// }
//}
//public static ITransaction GetSessionTransaction()
//{
// return GetCurrentSession().BeginTransaction();
//}
}
}