Opening multiple sessions simultaneously in NHibernate

I finally figured out what was wrong with my code, but I'm not sure how to fix it. I have some background processes running in a separate thread that do some database maintenance tasks. Here's an example of what's going on:

//Both processes share the same instance of ISessionFactory
//but open separate ISessions

//This is running on it own thread
public void ShortRunningTask()
{
    using(var session = _sessionFactory.OpenSession())
    {
        //Do something quickly here
        session.Update(myrecord);
    }
}

//This is running on another thread
public void LongRunningTask()
{
    using(var session = _sessionFactory.OpenSession())
    {
        //Do something long here
    }
}

      

Let's say I start first LongRunningTask

. During startup, I run ShortRunningTask

into a different thread. ShortRunningTask

ends and closes the session. As soon as it LongRunningTask

ends, it tries to do something with the created session, but an error occurs, saying that the session has already been closed.

It is clear what is happening that ISessionFactory.OpenSession () is not honoring the fact that I have opened 2 separate sessions. Closing a session opened at ShortRunningTask

also closes the session at LongRunningTask

How can I fix this? Please, help!

Thank!


UPDATE

Therefore, everyone seems to think that my correction is completely wrong. So, here's the configuration I'm using:

_sessionFactory = Fluently.Configure()
                  .Database(
                  FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
                  .ConnectionString(db => db.Is(
WikipediaMaze.Core.Properties.Settings.Default.WikipediaMazeConnection)))
                  .Mappings(m => m.FluentMappings.AddFromAssemblyOf<IRepository>())
                  .BuildSessionFactory();

      

I have no configuration in the XML file. Should be? What I am missing. Here's another example of how multiple sessions fail:

public void OpenMultipleSessionsTest()
{
    using(var session1 = _sessionFactory.OpenSession())
    {
        var user = session1.Get<Users>().ById(1);
        user.Name = "New Name";

        using(var session2 = _sessionFactory.OpenSession())
        {
            //Do some other operation here.
        }

        session1.Update(user);
        session1.Flush(); // Throws error 'ISession already closed!'
    }
}

      

+2


source to share


1 answer


I figured out how to fix this problem. I am setting my SessionFactory to be a singleton by creating it [ThreadStatic]

like this:

[ThreadStatic]
private ISessionFactory _sessionFactory;

[ThreadStatic]
private bool _isInitialized;

public ISessionFactory SessionFactory
{
    get
    {
        if(!_isInitialized)
        {
            //Initialize the session factory here
        }
    }
}

      



The crux of the problem is that creating sessions for separate threads from the same ISessionFactory is problematic. ISessionFactory doesn't like opening multiple ISessions at the same time. When closing, automatically closes all open ones. The attribute [ThreadStatic]

creates a separate ISessionFactory for each thread. This allows me to open and close ISessions on each thread without affecting others.

-2


source







All Articles