C#学习教程:数据库连接期间Nhibernate空引用exception分享


数据库连接期间Nhibernate空引用exception

当我的程序开始连接到Windows XP上的MS SQL 2005 EXPRESS DB时,我有时会收到此exception。 有时我得到这个错误,有时不… 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 { { 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。 这是对的吗?

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

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

在应用程序启动时执行此操作,您将遇到的问题要少得多。

上述就是C#学习教程:数据库连接期间Nhibernate空引用exception分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1031578.html

发表评论

邮箱地址不会被公开。 必填项已用*标注