Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3964870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:23:13+00:00 2026-05-20T03:23:13+00:00

I am getting this exception sometimes when my program is starting connection to MS

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:23:13+00:00Added an answer on May 20, 2026 at 3:23 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting the following error on execution of a multi-threading program java.lang.OutOfMemoryError: Java
We're getting this error java.lang.NullPointerException at java.util.ArrayList.<init>(Unknown Source) at de.mystuff.ExtendedArrayList.<init>(ExtendedArrayList.java:38) where ExtendedArrayList:38 is new
I have an XOR encypted file by a VB.net program using this function to
When making changes using SubmitChanges() , LINQ sometimes dies with a ChangeConflictException exception with
I have written a bunch of unit tests inside VS2010 Express and tests being
Let's say I have a projoct A with a class A that has this
I'm getting the memory leak message upon shutdown, saying that I'm leaking 3 of
I have this method for shrinking down an image for a website that I'm
I'm reading data from a table( from a MySQL Database) with Hibernate SQL Query.
UPD. 0xC0000005 is STATUS_ACCESS_VIOLATION , defined in winnt.h. Meaning the app has tried to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.