Каков наилучший подход к управлению сессиями NHibernate для использования в многопоточном приложении службы Windows?
У меня есть приложение службы Windows, которые работают с многопоточностью. Я использую NHibernate в уровне доступа к данным этого приложения.
Каково ваше предложение для управления сессиями в этом приложении. Я читал о UNHAddins, это хорошее решение?
2 ответа
Я использую встроенные в контекстные сессии NHibernate. Вы можете прочитать о них здесь:
http://nhibernate.info/doc/nhibernate-reference/architecture.html
Вот пример того, как я использую это:
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>
Я никогда не смотрел на 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) его ленивая реализация, так что вам не нужно запускать сеанс для каждого потока, если в этом нет необходимости. меньшее соединение с БД всегда лучше, имхо.