Randomly NHibernate seems to fail with an IndexOutOfRange Exception. The code works most of the, time but causes random application crashes.
public T GetByID<T>(Guid Id) where T : Modules.Common.EntityBase
{
try
{
ISession session = NHibernateHelper.GetCurrentSession();
var product = session
.CreateCriteria(typeof(T))
.Add(Restrictions.Eq("Id", Id))
.UniqueResult<T>();
return product;
}
catch (HibernateException ex)
{
NHibernateHelper.CloseSession();
throw;
}
}
I'm using the code on a WCF Service where ISessions are managed for each individual httpcontext, so i don't think it's due to thread safety. The Exception comes from DataReader so I'm going to guess that it is comming from the UniqueResult line.
Here is the get Current Session function
public static ISession GetCurrentSession()
{
if (HttpContext.Current == null)
{
lock (sessionLock)
{
if (_session == null)
_session = sessionFactory.OpenSession();
}
return _session;
}
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
currentSession = sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = currentSession;
}
return currentSession;
}