Archive

Archive for August, 2009

Modifying log4net settings

August 26th, 2009 No comments

Here’s how to modify log4net settings from a config file before initializing log4net. This can be useful for example if you have encrypted connection strings in App.config and have to decrypt them before passing them to log4net.

XmlElement log4NetSection = (XmlElement)ConfigurationManager.GetSection("log4net");

// Modify the xml, e.g. by adjusting the InnerXml property...
log4NetSection.InnerXml = StringCrypto.DecryptTaggedString(mypwd, log4NetSection.InnerXml);

XmlConfigurator.Configure(log4NetSection);

/Emil

Categories: .Net programming Tags:

Best practice for NHibernate: Don’t store references to ISession-instances

August 24th, 2009 2 comments

Do you see any (or all) of these exceptions (NHibernate.ADOException) in you NHibernate-based web application?

could not execute query
failed to lazily initialize a collection, no session or session was closed
Transaction not successfully started
The server failed to resume the transaction. Desc:86000002dc.

If you do, it might be a good idea to make sure you don’t store any ISession-references anywhere. If you do there’s a great risk that something goes wrong if two threads (i.e. two requests) are using the session at the same time as the NHibernate session implementation is not thread safe.

Instead, do this when an ISession is needed:

ISession session = SessionFactory.Factory.GetCurrentSession();

That code will get the session from the current context (which of course must be updated by your code, look here for more info).

/Emil

Categories: NHibernate Tags:
Web Analytics