数据库连接期间 Nhibernate 空引用异常 [英] Nhibernate null reference exception during database connection

查看:56
本文介绍了数据库连接期间 Nhibernate 空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时当我的程序开始连接到 Windows XP 上的 MS SQL 2005 EXPRESS DB 时,我会收到此异常.有时我会遇到这个错误,有时不会...... NHibernate v3

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 类在单独的线程上运行,但只能在一个线程上运行,我未来可以在多个线程上运行.

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

我有类似的 SessionProviderMsSql2005 类,但仅用于 SQLite 连接,它有相同的问题,sqlite 的会话提供程序在多个线程上运行...

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()

有我的 SessionProviderMsSql2005 类

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();
            }
        }
    }
}

我正在使用这个类:

 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?

更新:修正 SessionFactory getter.正确吗?

Update: correction of SessionFactory getter. Is it correct?

推荐答案

这可能是多线程问题,与 NHibernate 没有直接关系,请在此处查看相同问答:在多线程场景中调用 Dictionary 对象的 set_item 方法时抛出 NullReferenceException

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

这篇关于数据库连接期间 Nhibernate 空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆