[英]Nhibernate null reference exception during database connection
當我的程序開始連接到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吸氣劑的更正。 這是正確的嗎?
這可能是一個多線程問題,與NHibernate沒有直接關系,請在此處查看相同的問題並回答: 在多場景情況下,調用Dictionary對象的set_item方法時拋出NullReferenceException
不要懶惰地初始化會話工廠。
在應用程序啟動時執行此操作,您將遇到的問題要少得多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.