O'Reilly logo

NHibernate 3 Beginner's Guide by Gabriel N. Schenker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling exception

We can put it very short and simply: all NHibernate exceptions are non-recoverable!

NHibernate use might lead to exceptions, usually, HibernateException. This exception can have a nested inner exception. To access the root cause of the exception, we can and should use the InnerException property.

If an exception happens during a database operation, then the NHibernate session object is in an inconsistent state and should NOT be used any more. It is best to immediately rollback the active transaction, dispose the current session, and start over. Here is a code snippet we can use to deal with the possibility of exception happening:

var session = sessionFactory.OpenSession(); var transaction = session.BeginTransaction(); try { // ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required