1

I am getting this exception sometimes when my program is starting connection to MS SQL 2005 EXPRESS DB on Windows XP. Sometimes I got this error and sometimes not... NHibernate v3

DBMonitor class runs on separate thread, but only on one, i the future can run on multiple.

I have similar SessionProviderMsSql2005 class but only for SQLite connection and it has same issue and session provider for sqlite runs on multiple threads...

System.NullReferenceException: Object reference not set to an instance of an object.
   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 MySolution.DatabaseLayer.Repositories.SessionProviderMsSql2005.get_SessionFactory()
   at MySolution.DatabaseLayer.Repositories.SessionProviderMsSql2005.OpenSession()

There is my SessionProviderMsSql2005 class

using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Cfg;
using Environment = NHibernate.Cfg.Environment;

namespace MySolution.DatabaseLayer.Repositories
{
    public class SessionProviderMsSql2005
    {
        private static readonly object _padlock = new object();

        private static Configuration _configuration;
        public static Configuration Configuration
        {
            get
            {
                lock (_padlock)//must be thread save!
                {
                    if (_configuration == null)
                    {

                        if (string.IsNullOrEmpty(InitialCatalog))
                        {
                            throw new NullReferenceException("Property InitialCatalog could not be null or empty!");
                        }

                        if (string.IsNullOrEmpty(ServerName))
                        {
                            throw new NullReferenceException("Property ServerName could not be null or empty!");
                        }

                        _configuration = new Configuration();
                        _configuration.Configure("Config/Hibernate.MsSql2005.cfg.xml");
                        _configuration.AddAssembly("MySolution.DatabaseLayer");
                        Configuration.AddProperties(new Dictionary<string, string> 
                                                     {
                                                         { Environment.ConnectionString, @"Server="+ServerName+";initial catalog="+InitialCatalog+";Integrated Security=SSPI;" }
                                                     });
                    }
                    return _configuration;
                }
            }
        }

        private static ISessionFactory _sessionFactory;
        public static ISessionFactory SessionFactory
        {
            get
        {
            lock (_padlock)
            {
                if (_sessionFactory == null)
                {
                    lock (_padlock)
                    {
                        _sessionFactory = Configuration.BuildSessionFactory();
                    }
                }
                return _sessionFactory;
            }
        }
        }

        public static string ServerName { get; set; }
        public static string InitialCatalog { get; set; }


        private SessionProviderMsSql2005()
        { }

        public static ISession OpenSession()
        {
            lock (_padlock)
            {
                return SessionFactory.OpenSession();
            }
        }

        public static void CloseSession()
        {
            lock (_padlock)
            {
                SessionFactory.Close();
            }
        }
    }
}

I am using this class like this:

 public class DBMonitor
{
    private ISession _session;


    public DBMonitor(string serverName, string initialCatalog)
    {
        SessionProviderMsSql2005.ServerName = serverName;
        SessionProviderMsSql2005.InitialCatalog = initialCatalog;
    }

    public void Start()
    {

     _session = SessionProviderMsSql2005.OpenSession();

     IList data = _session.CreateSQLQuery("SELECT * FROM someTable WHERE id = 1").List();

      ...
     }
   }

Any idea what cause this?

Update: correction of SessionFactory getter. Is it correct?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Simon
  • 3,235
  • 5
  • 36
  • 47

2 Answers2

4

This is probably a multi-threading issue, not directly related to NHibernate, see the same question and answer here: Throw a NullReferenceException while calling the set_item method of a Dictionary object in a multi-threding scenario

Community
  • 1
  • 1
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • ok thank you, but I am using lock statement, what should I do more? – Simon Feb 09 '11 at 11:29
  • The getter on SessionFactory is not protected, can you try `lock (_padlock){Configuration.BuildSessionFactory().OpenSession;}` ? – Simon Mourier Feb 09 '11 at 12:47
  • can you see my update of SessionFactory getter?is it correct? or can you write for me, pls? – Simon Feb 09 '11 at 12:59
  • @Simon - hmmm... I can't really say. What I mean is could you try to put the whole failing path in the same lock (that's what I put in my previous comment), just try to do "Configuration.BuildSessionFactory().OpenSession" in the same lock, instead of splitting with this static property. – Simon Mourier Feb 09 '11 at 14:39
  • yes it was it, I was starting connection with DB's in few threads and NHibernate saves them to dictionary. So result is to inicialize one by one on the begining of app. – Simon Feb 22 '11 at 13:51
3

Don't initialize your session factory lazily.

Do it on application start and you'll have a lot less problems to deal with.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • I need one connection to SQLite DB, I could it initialize on the app start. Then I have few connection to MS SQL 2005 DB, depend on the app settings. Right now I have only one connection but in the future I can have more. This connection I am starting in threads, depends on the app settings. So I will have more SessionProviderMsSql2005 with different settings each one on the thread. SessionProviderMsSql2005 class is used only in one thread this time. – Simon Feb 09 '11 at 16:00
  • You are mixing the terms "connection" and session factory. – Diego Mijelshon Feb 09 '11 at 16:35