These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: NHibernate Session Management?
PostPosted: Thu Feb 12, 2009 11:17 am 
Beginner
Beginner

Joined: Fri Jul 06, 2007 9:27 pm
Posts: 22
What is the best way to manage sessions in a DDD application?

Most of the sample code that I have seen shows an NHibernate ISession object being created by code in a business layer and then passed to a data provider, which executes a query. The business layer disposes of the ISession object when it's done with the object graph that the ISession supports.

I don't like this approach, because it pollutes the business layer with data access concerns. If I ever need to change my data access approach, I'll have to go back and do a major rewrite of my business layer. I'd prefer to encapsulate all of my data access code in a separate layer, and isolate the business layer from any knowledge of NHibernate.

That creates some pretty obvious problems for session management, when using the 'session per conversation' approach. An ISession will need a lifespan longer than the life of a particular data access method. That is easily accomplished by creating the ISession as a member-level variable of my data access project. But how do I dispose of the session when I no longer need it?

I have been searching for an article or a blog post on this subject, but I am not having any luck so far. Any thoughts or suggestions greatly appreciated.

David Veeneman
Foresight Systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 5:14 pm 
Beginner
Beginner

Joined: Fri Jul 06, 2007 9:27 pm
Posts: 22
I found my answer. NHibernate 1.2 provides an ISessionFactory.GetCurrentSession() method, which returns the session instance associated with the current persistence context, similar to the ASP.NET notion of an HTTP request context.

NHibernate's session-context implementation is documented in Chapter 10 of NHibernate In Action, which discusses the creation of a persistence layer which isolates data access concerns from the rest of an application. It's a good explanation, and I recommend it to anyone who is integrating NHibernate into a Domain-Driven Design (DDD) application.

David Veeneman
Foresight Systems


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.