Derik Whittaker

Syndication

News


Debugging NHibernate Exceptions

When developing your data access layer with NHibernate you are bound to run into some pretty nasty exceptions.  Many times the exceptions may be nasty, but at least they explain the problem in enough detail to allow you to solve the problem.  But what do we do when the exceptions do not convey enough information to allow us to simply and easily spot our issue?  Simply, time to bust out the shovel and pick ax and start digging for the nugget of information which will allow you to find your solution.

The good news is there is at least 2 (could be many more) ways you can get detailed information on what NHibernate is doing and what possible be wrong.

Utilize NHibernate Profiler
If you are using NHibernate and you are NOT using NHProfiler then you are basically writing code with one hand tied behind your back.  This tool can and will help you solve so many problems you will be amazed. 

But when hunting bugs NHProf is practically useful because it gives you a very clean way to see the exact sql which is being generated and executed in real time (or damn close to it).  Implementing NHprof could not be easier and takes a mere 3 steps and 1 line of code.

To Implement all you do is

  1. Add reference to HibernatingRhinos.NHibernate.Profiler.Appender.dll class in your project.
  2. Add this one line of code at the entry point of your application (or unit test)
    HibernatingRhinos.NHibernate.Profiler.Appender.NHibernateProfiler.Initialize();
  3. Launch the GUI HibernatingRhinos.NHibernate.Profiler.Client.exe

Utilize the logging which is build into NHibernate via Log4Net 
Now sometimes even NHProfiler is not going to solve your problems and when this is the case you are in for some fun.  I have found the best way to debug what is going on is to turn on logging for Nhibernate.  Now I am not going to go into depth on how to do this as post on Hibernateing Rhinos and Nhiberate.org do a great job of providing this information.

Once you have your app.config or your web.config setup all you need to do is add this line of code at the entry point of your application (or unit tests)
log4net.Config.XmlConfigurator.Configure()

When implementing logging with Log4Net I would suggest 2 things

  • Turn on DEBUG logging, this will produce a ton of content but is needed
  • If using a file appender up the max file size to be at least 1mb. Anything lower will simply produce too many files and you will be swimming in a see of log files

If you utilize the above 2 techniques for trouble shooting/debugging your nhibernate issues you should be able to resolve most of them in no time flat. 

Till next time,


Posted 05-05-2009 4:14 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

joe wrote re: Debugging NHibernate Exceptions
on 05-05-2009 12:31 PM

NHProf needs some kind of  hobbyist / oss license (perhaps less features?).  The cost is simply too high for anything outside of a corporate environment.  It is a fantastic tool, but to me at least, the cost is a huge barrier to entry.

Also, I had a question about log4net and nhibernate.  Maybe it has something to do with my configuration, but if i set the output level to "ALL" -- nothing is being written out.  Have you run into anything like this?  Changing it to DEBUG works though.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)