-->
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.  [ 3 posts ] 
Author Message
 Post subject: Question on session opening and closing
PostPosted: Fri Aug 03, 2007 6:01 am 
Newbie

Joined: Tue Jul 31, 2007 8:35 am
Posts: 3
Having read the free chapter 2 of the Hibernate book, I understand how they open a session, perform a unit of work, then close the session. gurther more this ties in nicely with the default save/delete methods in the facades that work in the same way.

What I'm not sure about is when you create a findByxxxxx method in the facade, you open the session, perform your query, receiving a List of Object back and return that from your findBy method.

Initially I wasn't closing the session at the end of the findBy method, everything worked fine in the jsp/java, but later in the running of the program I would get errors regarding exitsing references or similar, often these could be solved by adding a session.clear.

However I eventually got a "Found two representations of same collection when flush called" error in the save method of a facade, that only occurred once the method had been used once. I found that a session close as the first command solved the problem.

So I thought maybe I should be closing the session at the end of the findBy methods too. However when I tried this, I got errors from the JSP when it was trying to work with the list returned from findBy, the error stated the session was closed, so obviously I shouldn't closed the session.

Anyone able to give me some pointers here?

Tom


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 10:07 am 
Newbie

Joined: Tue Mar 06, 2007 1:35 pm
Posts: 10
Hi Tom.

This is one of the most popular controversy available on the Hibernate usage: where to open and where to close your sessions. AND, if you're working with a web-based MVC, there's even more to consider.

The fact is that you should close your session after using it in a way or another, sessions holds connections, transactions, cached objects, and lots of features and those are managed when you open and close it.

I'm working heavily with Hibernate on a project right now. We have adopted an approach where my DAO factory has no idea of there it's session came from, it's constructor injected by it's caller. This solves part of the problem.

Also, we're using an MVC framework called VRaptor (http://www.vraptor.org/) that comes with some solutions that allows you to delegate the session's opening and closing to interceptors. It gives you freedom to transparently handle lazy lists and attributes on your jsp's...

Is that any help? Hope so!

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 10:55 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
All of this stuff is explained in detail on http://hibernate.org/42.html

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.