I am using Castle Active Record and NHibernate and need to be able to run HQL updates without using session. When I use session then it ends up causing locking issues:
public static ISession GetSession(out ISessionFactoryHolder factoryHolder, out bool created)
{
created = false;
var type = typeof(T);
factoryHolder = ActiveRecordMediator.GetSessionFactoryHolder();
ISessionScope activeScope = factoryHolder.ThreadScopeInfo.GetRegisteredScope();
ISession session = null;
var key = factoryHolder.GetSessionFactory(type);
if (activeScope == null)
{
created = true;
session = factoryHolder.CreateSession(type);
}
else
{
if (activeScope.IsKeyKnown(key))
session = activeScope.GetSession(key);
else
session = factoryHolder.GetSessionFactory(type).OpenSession();
}
return session;
}
public static void UpdateQuery(string query)
{
ISessionFactoryHolder factoryHolder;
var created = false;
var session = GetSession(out factoryHolder, out created);
session.CreateQuery(query).ExecuteUpdate();
if (created)
factoryHolder.ReleaseSession(session);
}
This is causing significant issues in our production environment due to all of the locks happening from the sessions. How would I just run hql updates without instantiating session?
You cannot execute HQL outside of an NHibernate session. However you might find that using a stateless session will help (use a StatelessSessionScope in ActiveRecord).
Alternatively if the Stateless session is still not performant enough, you will have to execute your query using straightforward SQL, using session.CreateSqlQuery(...)