How to configure NHibernate with Sharp architecture in a multi-threaded console application?

I am still pretty new to these technologies. The real issue here is how to manage sessions per thread in a console application. Currently, if I run it as a single thread, everything is fine. As soon as I switch to a multi-threaded model, I will begin to see rivalry at the session level (because the Session object is not an adadsafe in design). KeyNotFound exceptions (among other things) begin to throw.

In a web application, you would do something like this:

/// <summary> /// Due to issues on IIS7, the NHibernate initialization cannot reside in Init() but /// must only be called once. Consequently, we invoke a thread-safe singleton class to /// ensure it only initialized once. /// </summary> protected void Application_BeginRequest(object sender, EventArgs e) { NHibernateInitializer.Instance().InitializeNHibernateOnce( () => InitializeNHibernateSession()); } /// <summary> /// If you need to communicate to multiple databases, you'd add a line to this method to /// initialize the other database as well. /// </summary> private void InitializeNHibernateSession() { var path = ConfigurationManager.AppSettings["NHibernateConfig"]; NHibernateSession.Init( webSessionStorage, new string[] { Server.MapPath("~/bin/foo.Data.dll") }, new AutoPersistenceModelGenerator().Generate(), Server.MapPath("~/App_Configuration/" + path )); } // sample of my console app... very simple static void Main(string[] args) { InitializeNHibernateSession(); while(true) { Task.Factory.StartNew(() => SomeAwesomeLongRunningPieceOfWork()); } } 

Which essentially performs initialization once per thread (web request) in global.asax.

Any ideas on how to set this (session management) in a console application?

+4
source share
1 answer

This worked for me:

 // Three threads: for (int i = 0; i < 3; i++) { Thread curThread = new Thread(StartThread); curThread.Start(); } private void StartThread() { NHibernateInitializer.Instance().InitializeNHibernateOnce(InitializeNHibernateSession); SomeAwesomeLongRunningPieceOfWork(); } private void InitializeNHibernateSession() { var path = ConfigurationManager.AppSettings["NHibernateConfig"]; NHibernateSession.Init( new ThreadSessionStorage(), new string[] { "foo.Data.dll" }, new AutoPersistenceModelGenerator().Generate(), "./App_Configuration/" + path); } 

The key was this class, which I got from:

http://groups.google.com/group/sharp-architecture/browse_thread/thread/ce3d9c34bc2da629?fwc=1 http://groups.google.com/group/sharp-architecture/browse_thread/thread/51794671c91bc5e9/386efc30d4c0bf16#386efcdfcdfdd38fef30d4c0ff16#386eff30f4feffdf38fef3030

 public class ThreadSessionStorage : ISessionStorage { [ThreadStatic] private static ISession _session; public ISession Session { get { return _session; } set { _session = value; } } public ISession GetSessionForKey(string factoryKey) { return Session; } public void SetSessionForKey(string factoryKey, ISession session) { Session = session; } public IEnumerable<ISession> GetAllSessions() { return new List<ISession>() { Session }; } } 

And it works great.

+7
source

Source: https://habr.com/ru/post/1369101/


All Articles