multithreading - Opening multiple sessions simultaneously in NHibernate -
Finally, I thought what is wrong with my code, but I'm not sure how to fix it. I have some background processes Those that are running on a separate thread do some database maintenance tasks. Here's an example of what's happening:
// An example example of both processes is fascingitry / but open different gestures // it is running on its thread, Public Zero Short Rints () {(Var Session = _sessionFactory.OpenSession ()) {// Some quick sessions here Update (MyCard); }} // This is the public running on another thread LongRunningTask () ({var session = _sessionFactory.OpenSession ()) {// Do some time here}}
We say First of all, let's start the longing task
, although this is going on, I start the ShortRunningTask
on the second thread. Shortcutting Task
ends and closes its sessions once the LongRunningTask
ends, something to do with it, but there is an error This session has already been closed.
Obviously this is happening that ISessionFactory.OpenSession () is not respecting the fact that I have opened 2 separate sessions, the session opened in Shortcutting Tasks
Closing can also close the session in LongRunningTask
How can I fix this? Please help!
Thank you!
UPDATE
So everyone thinks that my fix is completely wrong. So here's the configuration:
_sessionFactory = Flu. Configure (.db) (FluNet NHbernate.cg.db.mscQL configuration mssql-2008 .ConnectionString (db = & gt; db.is (WikipediaMaz.Core.properties.setting.f default Wikipedia mood connection)). Mappings (M => M. Flume Mappings. AdfomassandAfA & lt; IRAPSorgetatory & gt; ()). BuilderSensfactory ();
I have no configuration in any XML file. Should I be there? What am I missing. Here's another example of how to open multiple sessions:
Public Zero Open Multileast () {(var session1 = _sessionFactory.OpenSession ()) {var user = session1.Get & Lt; Users & gt; () ById (1); User.Name = "new name"; (Var session2 = _sessionFactory.OpenSession ()) {// do another operation here} Session1.Update (user); Session1.Flush (); // throws error 'ISession already stopped!' }}
I came to know how to fix the problem. I was set up as a session to set up my session in the form of a session [threadstatic]
like this:
[threadstatic] Private organizationfrequency _sessionFactory ; [Thread Static] Private Bull _is Initial; Public IsessionFactory SessionFactory {get {if (! _InInitialized) {// Start Session Function}}} The problem is the problem that creating session on separate threads from the same ISessionFactory is problematic. ISessionFactory is not going to open more than one ISIS at the same time. One closes, automatically closes any other which is open. Using the [ThreadStatic]
attribute creates a separate ISessionFactory for each thread. This allows me to open and close the Christians on every thread without affecting others.
Comments
Post a Comment