マルチスレッドで動作するWindowsサービスアプリケーションがあります。このアプリケーションのデータアクセス層でNHibernateを使用しています。
このアプリケーションでのセッション管理についてのあなたの提案は何ですか。UNHAddinsについて読みましたが、それは良い解決策ですか?
マルチスレッドで動作するWindowsサービスアプリケーションがあります。このアプリケーションのデータアクセス層でNHibernateを使用しています。
このアプリケーションでのセッション管理についてのあなたの提案は何ですか。UNHAddinsについて読みましたが、それは良い解決策ですか?
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();
}
}
これに加えて、休止状態の構成ファイルに次のものがあります。
<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) 遅延実装であるため、必要でない場合はスレッドごとにセッションを開始する必要がないことです。データベースへの接続が少ないほど、常に優れています。