我正在使用 Castle Active Record 和 NHibernate,并且需要能够在不使用会话的情况下运行 HQL 更新。当我使用会话时,它最终会导致锁定问题:
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);
}
由于会话中发生的所有锁定,这在我们的生产环境中引起了重大问题。我如何在不实例化会话的情况下运行 hql 更新?