c# - Nhibernate null referrence exception during database connection -
i getting exception when program starting connection ms sql 2005 express db on windows xp. got error , not... nhibernate v3
dbmonitor class runs on separate thread, on one, future can run on multiple.
i have similar sessionprovidermssql2005 class sqlite connection , has same issue , session provider sqlite runs on multiple threads...
system.nullreferenceexception: object reference not set instance of object. @ system.collections.generic.dictionary`2.insert(tkey key, tvalue value, boolean add) @ system.collections.generic.dictionary`2.set_item(tkey key, tvalue value) @ nhibernate.impl.sessionfactoryobjectfactory.addinstance(string uid, string name, isessionfactory instance, idictionary`2 properties) @ nhibernate.impl.sessionfactoryimpl..ctor(configuration cfg, imapping mapping, settings settings, eventlisteners listeners) @ nhibernate.cfg.configuration.buildsessionfactory() @ mysolution.databaselayer.repositories.sessionprovidermssql2005.get_sessionfactory() @ mysolution.databaselayer.repositories.sessionprovidermssql2005.opensession()
there 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 { { lock (_padlock)//must thread save! { if (_configuration == null) { if (string.isnullorempty(initialcatalog)) { throw new nullreferenceexception("property initialcatalog not null or empty!"); } if (string.isnullorempty(servername)) { throw new nullreferenceexception("property servername not 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 { { 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 using class 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 * sometable id = 1").list(); ... } }
any idea cause this?
update: correction of sessionfactory getter. correct?
this multi-threading issue, not directly related nhibernate, see same question , answer here: throw nullreferenceexception while calling set_item method of dictionary object in multi-threding scenario
Comments
Post a Comment