【问题标题】:Nhibernate null reference exception during database connection数据库连接期间的休眠空引用异常
【发布时间】:2011-02-09 09:00:32
【问题描述】:

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

DBMonitor 类在单独的线程上运行,但只能在一个线程上运行,我将来可以在多个线程上运行。

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

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 类

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

      ...
     }
   }

知道是什么原因造成的吗?

更新:更正 SessionFactory getter。对吗?

【问题讨论】:

  • 您是否在多个线程中使用此代码?
  • 是的,DBMonitor 类在单独的线程上运行,但只能在一个线程上运行,我将来可以在多个线程上运行。

标签: c# nhibernate


【解决方案1】:

这可能是一个多线程问题,与 NHibernate 没有直接关系,在此处查看相同的问题和答案:Throw a NullReferenceException while calling the set_item method of a Dictionary object in a multi-threding scenario

【讨论】:

  • 好的,谢谢,但是我正在使用lock语句,我该怎么办?
  • SessionFactory上的getter没有被保护,可以试试lock (_padlock){Configuration.BuildSessionFactory().OpenSession;}吗?
  • 你能看到我对 SessionFactory getter 的更新吗?对吗?或者你能为我写信吗?
  • @Simon - 嗯......我真的不能说。我的意思是你可以尝试将整个失败的路径放在同一个锁中(这就是我在之前的评论中提出的),只是尝试在同一个锁中执行“Configuration.BuildSessionFactory().OpenSession”,而不是与这个静态属性。
  • 是的,我在几个线程中开始与数据库连接,NHibernate 将它们保存到字典中。所以结果是在应用程序开始时一一初始化。
【解决方案2】:

不要懒惰地初始化你的会话工厂。

在应用程序启动时执行此操作,您需要处理的问题会少很多。

【讨论】:

  • 我需要一个到 SQLite DB 的连接,我可以在应用启动时对其进行初始化。然后我几乎没有连接到 MS SQL 2005 DB,这取决于应用程序设置。现在我只有一个连接,但将来我可以有更多。我在线程中开始的这个连接取决于应用程序设置。所以我会有更多的 SessionProviderMsSql2005,每个线程都有不同的设置。 SessionProviderMsSql2005 类这次只在一个线程中使用。
  • 您将“连接”和会话工厂这两个术语混用了。
猜你喜欢
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-25
  • 2015-03-04
  • 2014-06-12
  • 2015-10-01
  • 1970-01-01
相关资源
最近更新 更多