-->
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.  [ 6 posts ] 
Author Message
 Post subject: finalize() and session.close()
PostPosted: Mon Mar 01, 2004 2:53 pm 
Newbie

Joined: Wed Feb 04, 2004 7:12 am
Posts: 18
Location: Brasilia - Brazil
I have some doubt about closing session after its use.
I don't wanna have to close the session explicity, because I'm using DAO Design Pattern, so I was thinking in something like this:

What about to close the session in the finalize() method?

Code:
protected void finalize() throws Throwable{
    try {
              //close session
      }
      }catch (Exception ex) {}
  }


This will avoid the problems related to lazy loading, because the session will be closed before garbage collection, and at this time, probably this object is not used anymore.

Is there some problem with this aproach?

Thanks a lot,

_________________
___________________________
[ MediaSonic (www.geleira.org) ]


Top
 Profile  
 
 Post subject: possible problems:
PostPosted: Mon Mar 01, 2004 5:09 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
finalize can't be relied on.

It will take two garbage collection cycles to collect an object which has a finalize.

Check out Gavin's blog regarding this here:
http://blog.hibernate.org/cgi-bin/blosxom.cgi/2004/02/18/


Top
 Profile  
 
 Post subject: Open session in view?
PostPosted: Mon Mar 01, 2004 5:11 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Have you considered using the "Open Session In View" pattern?


Top
 Profile  
 
 Post subject: Re: Open session in view?
PostPosted: Tue Mar 02, 2004 7:47 am 
Newbie

Joined: Wed Feb 04, 2004 7:12 am
Posts: 18
Location: Brasilia - Brazil
DavidNDuffy wrote:
Have you considered using the "Open Session In View" pattern?


Hi David, thanks for the advice, but, my idea is to make the persistence layer transparent of implementation, for example, now I'm using Hibernate but in the future I could be using another techology, thus, if a use this pattern I'll have Hibernate specific code.

I was thinking about to add a method release() to my AbstractDAO, so all child class will implement it releasing any resource that have been alocated, in case of Hibernate the session and JDBC (for example) the connection.

_________________
___________________________
[ MediaSonic (www.geleira.org) ]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 8:31 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The Open Session in View pattern DOES NOT create any dependency.

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 8:34 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I guess I have to update pattern page (it is quite a mess anyway) to make this clear. Search the forum for discussions about this, here is a recent thread: http://forum.hibernate.org/viewtopic.php?t=927886

_________________
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.  [ 6 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.