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

Creating your first session

Creating a NHibernate session is very easy. Once we have a sessionFactory object to hand, we can just call the OpenSession method on it:

var session = sessionFactory.OpenSession();

Usually, we might want to open a session in a using statement to guarantee that the session is closed and disposed, no matter what the result of the operation is. We might also want to start a transaction for better predictability of the outcome of the operation. Remember, a transaction can either fail as a whole or succeed as a whole; no other outcome is possible. This makes it easier for us to write our application:

using (var session = sessionFactory.OpenSession()) { using (var transaction = session.BeginTransaction()) { // create, update, ...

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