-->
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.  [ 5 posts ] 
Author Message
 Post subject: Using Servlet filters to close Sessions
PostPosted: Thu Sep 02, 2004 6:31 pm 
Newbie

Joined: Thu Aug 05, 2004 11:00 pm
Posts: 7
Location: New York, NY
Hibernate Version: 2.1

Mapping documents: generic

Code between sessionFactory.openSession() and session.close(): generic DAO code

Name and version of the database you are using: MySQL 4

Debug level Hibernate log excerpt: debug; N/A

I remember somebody else posted a question about closing a Hibernate Session with a Servlet filter and whether or not it violated the J2EE specification (which I think it did). It seems to be the recommended strategy (for now) and it does "work on all platforms".

But I just don't get a good feeling about a design where you open a session in the DAO code and then have to remember to close it somewhere in some other unrelated code. Does this bother other developers, too?

Is there any elegant strategy that anyone can think of for management of the Hibernate Session lifecycle?

I'm considering implementing and using ServletRequestListeners that could open and close Hibernate Sessions automatically, but this ends up binding Hibernate code into the Servlet Actions, which I don't like.

I'm a beginning J2EE developer, so I'm rather unsure of myself, but I'm willing to put a little time into implementing (and donating) an elegant, reusable solution if I can be sure my basic strategy will be successful.

Does the idea I've laid out above strike any of you gurus as promising? Should I give it a try?

_________________
When asked by a reporter what he thought of western civilization, Mahatma Ghandi once replied that he thought it would be a good idea.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 5:57 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Quote:
But I just don't get a good feeling about a design where you open a session in the DAO code and then have to remember to close it somewhere in some other unrelated code. Does this bother other developers, too?


What makes you think that it is closed by "unrelated code"? The code that closes the Session was specifically written for that purpose. Its like a Listener that is called when an Event was executed, nothing else. There is nothing bad about this.

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


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 5:58 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
And it doesn't violate any specification, as far as I know. (Please quote the paragraph and spec version whenever you say something like this.)

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


Top
 Profile  
 
 Post subject: Violate J2EE spec?
PostPosted: Wed Nov 03, 2004 5:29 am 
Newbie

Joined: Sat Jul 17, 2004 5:13 pm
Posts: 8
Location: Edegem, Belgium
christian wrote:
And it doesn't violate any specification, as far as I know. (Please quote the paragraph and spec version whenever you say something like this.)


Christian,

I think he referred to this post:
<http://forum.hibernate.org/viewtopic.php?t=933680&start=0&postdays=0&postorder=asc&highlight=servlet+filter+j2ee>

Regards,

Jos


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 03, 2004 2:59 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
So, use an interceptor of your J2EE server, its the same. Out of the spec, but works.

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