我有一个可以使用多线程的Windows服务应用程序。我在这个应用程序的数据访问层使用NHibernate。
您对此应用程序中的会话管理有何建议?我读到了UNHAddins,这是一个很好的解决方案吗?
答案 0 :(得分:17)
我使用NHibernate内置的上下文会话。你可以在这里阅读它们:
http://nhibernate.info/doc/nhibernate-reference/architecture.html#architecture-current-session
以下是我如何使用它的示例:
public class SessionFactory
{
protected static ISessionFactory sessionFactory;
private static ILog log = LogManager.GetLogger(typeof(SessionFactory));
//Several functions omitted for brevity
public static ISession GetCurrentSession()
{
if(!CurrentSessionContext.HasBind(GetSessionFactory()))
CurrentSessionContext.Bind(GetSessionFactory().OpenSession());
return GetSessionFactory().GetCurrentSession();
}
public static void DisposeCurrentSession()
{
ISession currentSession = CurrentSessionContext.Unbind(GetSessionFactory());
currentSession.Close();
currentSession.Dispose();
}
}
除此之外,我在hibernate配置文件中还有以下内容:
<property name="current_session_context_class">thread_static</property>
答案 1 :(得分:4)
我从来没有看过unhaddins,但这是我用于wcf的东西,应该也可以用于我想象的多线程一般的东西。
这是会话上下文:
namespace Common.Infrastructure.WCF
{
public class NHibernateWcfSessionContext : ICurrentSessionContext
{
private readonly ISessionFactoryImplementor factory;
public NHibernateWcfSessionContext(ISessionFactoryImplementor factory)
{
this.factory = factory;
}
/// <summary>
/// Retrieve the current session for the session factory.
/// </summary>
/// <returns></returns>
public ISession CurrentSession()
{
Lazy<ISession> initializer;
var currentSessionFactoryMap = OperationContext.Current.InstanceContext.Extensions.Find<NHibernateContextManager>().SessionFactoryMaps;
if (currentSessionFactoryMap == null ||
!currentSessionFactoryMap.TryGetValue(factory, out initializer))
{
return null;
}
return initializer.Value;
}
/// <summary>
/// Bind a new sessionInitializer to the context of the sessionFactory.
/// </summary>
/// <param name="sessionInitializer"></param>
/// <param name="sessionFactory"></param>
public static void Bind(Lazy<ISession> sessionInitializer, ISessionFactory sessionFactory)
{
var map = OperationContext.Current.InstanceContext.Extensions.Find<NHibernateContextManager>().SessionFactoryMaps;;
map[sessionFactory] = sessionInitializer;
}
/// <summary>
/// Unbind the current session of the session factory.
/// </summary>
/// <param name="sessionFactory"></param>
/// <returns></returns>
public static ISession UnBind(ISessionFactory sessionFactory)
{
var map = OperationContext.Current.InstanceContext.Extensions.Find<NHibernateContextManager>().SessionFactoryMaps;
var sessionInitializer = map[sessionFactory];
map[sessionFactory] = null;
if (sessionInitializer == null || !sessionInitializer.IsValueCreated) return null;
return sessionInitializer.Value;
}
}
}
这是上下文管理器:
namespace Common.Infrastructure.WCF
{
class NHibernateContextManager : IExtension<InstanceContext>
{
public IDictionary<ISessionFactory, Lazy<ISession>> SessionFactoryMaps = new Dictionary<ISessionFactory, Lazy<ISession>>();
public void Attach(InstanceContext owner)
{
//We have been attached to the Current operation context from the ServiceInstanceProvider
}
public void Detach(InstanceContext owner)
{
}
}
}
要清楚,正如另一个答案所述,线程静态上下文将开箱即用。我在这里的主要优点是1)你可以控制,2)它是一个懒惰的实现,所以你不必为每个线程启动一个会话,如果没有必要的话。与数据库的连接总是更好,imho。