Free Nhibernate configuration error in a multi-threaded application

I created a multithreaded application for IIS (ASP.NET MVC). When the streaming server is running, it creates 10 threads and processes work items in the streams.

Usually my application works well, but for some time I have errors, and I am sure that the problem comes from a good configuration. And I'm sure I was wrong again :)

Here is my SessionFactory class:

public class NHibernateHelper
{
    private static ISessionFactory sessionFactory;

    /// <summary>
    /// SessionFactory is static because it is expensive to create and is therefore at application scope.
    /// The property exists to provide 'instantiate on first use' behaviour.
    /// </summary>
    private static ISessionFactory SessionFactory
    {
        get
        {
            if (sessionFactory == null)
            {
                sessionFactory = CreateSessionFactory();
            }
            return sessionFactory;
        }
    }


    /// <summary>
    /// CreateSessionFactory
    /// </summary>
    /// <returns></returns>
    private static ISessionFactory CreateSessionFactory()
    {
        IPersistenceConfigurer dbConfigurer = MsSqlConfiguration.MsSql2005
            .ConnectionString("connection string ..")
                            .Cache(c => c
                                .UseQueryCache()
                                .ProviderClass<NoCacheProvider>()
                    ).ShowSql()
                    .CurrentSessionContext<ThreadStaticSessionContext>(); 
                    return Fluently
                            .Configure()
                            .Database(dbConfigurer)
                            .Mappings(mc =>
                            {
                                mc.FluentMappings.Add(typeof(UserMap));
                                mc.FluentMappings.Add(typeof(ApplicationMap));
                                mc.FluentMappings.Add(typeof(SubscriptionsMap));
                            })
                        .BuildSessionFactory();
    }


    public static ISession GetCurrentSession()
    {
        if (!CurrentSessionContext.HasBind(SessionFactory))
        {
            CurrentSessionContext.Bind(SessionFactory.OpenSession());
        }
        return SessionFactory.GetCurrentSession();
    }



    public static void DisposeSession()
    {
        var session = GetCurrentSession();
        session.Close();
        session.Dispose();
    }

    public static void BeginTransaction()
    {
        GetCurrentSession().BeginTransaction();
    }

    public static void CommitTransaction()
    {
        var session = GetCurrentSession();
        if (session.Transaction.IsActive)
            session.Transaction.Commit();
    }

    public static void RollbackTransaction()
    {
        var session = GetCurrentSession();
        if (session.Transaction.IsActive)
            session.Transaction.Rollback();
    }
}

Each thread calls the NHibernateHelper class with this line within itself;

            var myobjectinstance = new ObjectInstance();
            NHibernateHelper.GetCurrentSession().Save( myobjectinstance );

I saw that when I started the server for a while, it successfully deployed 300,000 work items for the purpose of testing. But once he gives errors of about 2-3 workers.

The exception is:

[0] = {"An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.\r\n\r\n"}

Internal selection:

Object reference not set to an instance of an object.

Internal exception stack trace:

    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
    at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
    at NHibernate.Impl.SessionFactoryObjectFactory.AddInstance(String uid, String name, ISessionFactory instance, IDictionary`2 properties)
    at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
    at NHibernate.Cfg.Configuration.BuildSessionFactory()
    at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() 
    in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 93

Any suggestion or help is appreciated.

+5
1

, CreateSessionFactory . sessionFactory , :

private static ISessionFactory SessionFactory
{
    get
    {
        if (sessionFactory == null)
        {
            sessionFactory = CreateSessionFactory();
        }
        return sessionFactory;
    }
}

. , singleton.

+7

All Articles