Hibernate version:1.2.0.GA
We have recently gone live with an application using NHibernate. I am experiencing a problem on the production server that I have not experienced on our dev server; I suspect the major difference is simply the number of users hitting the application at the same time.
The exception is: could not execute query
[ select staffmembe0_.StaffMemberID ... etc
The inner exception is: There is already an open DataReader associated with this Command which must be closed first.
Below is the query method and stack trace, followed by an NHibernateHelper utility class which handles opening and closing of the NHibernate sessions.
I believe that I am closing my sessions correctly, yes? Is there any other reason this error could be triggered?
1. My query method:
Code:
string hqlQuery = "from StaffMember stm "
+ "left join fetch stm.DepartmentPosition dpo "
+ "left join fetch dpo.PositionType ptp "
+ "left join fetch dpo.Department dpt "
+ "left join fetch stm.Professional pro "
+ "left join fetch pro.Person per "
+ "where stm.UserName = :UserName";
StaffMember staffMember = null;
try
{
ISession session = NHibernateHelper.GetCurrentSession();
staffMember = (StaffMember)session.CreateQuery(hqlQuery)
.SetString("UserName", userName)
.UniqueResult<StaffMember>();
}
catch (HibernateException hEx)
{
ExceptionManager.HandleException(this, hEx);
}
catch (Exception ex)
{
ExceptionManager.HandleException(this, ex);
}
finally
{
NHibernateHelper.CloseSession();
}
2. Stack trace:
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet querySpaces, IType[] resultTypes)
at NHibernate.Hql.Classic.QueryTranslator.List(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters, IList results)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
at NHibernate.Impl.QueryImpl.List()
at NHibernate.Impl.AbstractQueryImpl.UniqueResult[T]()
at KnowledgeTech.Umby.Repository.NHibernateRepositories.StaffMemberRepositoryNHibernate.FindByUserName(String domainAndUserName)
3. NHibernateHelper class:
public class NHibernateHelper
{
private static readonly ISessionFactory sessionFactory;
private static ISession _currentSession;
static NHibernateHelper()
{
sessionFactory = new Configuration().Configure().BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
if (_currentSession == null || !_currentSession.IsOpen)
{
_currentSession = sessionFactory.OpenSession();
}
return _currentSession;
}
public static void CloseSession()
{
_currentSession.Close();
}
}
Thanks,
David