-->
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.  [ 8 posts ] 
Author Message
 Post subject: open session in view
PostPosted: Fri Nov 05, 2004 9:38 pm 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
How to implement this pattern with servlet filters?

At the moment i have a filter class that perform session.connect / session.disconnect. Where session is retrieved with thread local variable to ensure that it is the same hibernate session during all the httpsession.
I also have a session listener (servlet session!) to put the hibernate session in the thread.

But this pattern is not effcient because it ask a connection to the db everytime also to do things that don't need a connection to the database. How to code it better?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 5:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You are using a database pool arent you? So your connections are open all the time anyways.


Top
 Profile  
 
 Post subject: Re: open session in view
PostPosted: Sat Nov 06, 2004 5:51 am 
Senior
Senior

Joined: Wed Aug 27, 2003 4:08 am
Posts: 178
Location: Wiesbaden, Germany
agori wrote:
How to implement this pattern with servlet filters?

At the moment i have a filter class that perform session.connect / session.disconnect. Where session is retrieved with thread local variable to ensure that it is the same hibernate session during all the httpsession.


You surely mean HTTP Request?
agori wrote:
I also have a session listener (servlet session!) to put the hibernate session in the thread.
But this pattern is not effcient because it ask a connection to the db everytime also to do things that don't need a connection to the database. How to code it better?


If you would use IoC framework ( like nanocontainer / nanowar ) your hibernate session would be created only when needed...

_________________
Got new hibernate xdoclet plugin? http://www.sourceforge.net/projects/xdoclet-plugins/
... Momentan auf der Suche nach neuen Projekt ode Festanstellung....


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 6:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you have to know that the open session in view pattern does not open the underlying jdbc connextion at each request.

when you are obtaining a session, it is not using a jdbc connection until you're really using it (querying, reattaching,...).

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 8:29 am 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
session.reconnect() does not open connection?
It only obtains a connection from the connection pooler wihtout open the connection, isn't it?
Anyway the session I think blocks a connection that can't be obtained in the same time from another thread.


Top
 Profile  
 
 Post subject: Re: open session in view
PostPosted: Sat Nov 06, 2004 9:20 am 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
You surely mean HTTP Request?

No.
My HTTP session store a hibernate session which i reconnect and disconnect everytime the filter is invoked. I don't have detached object so.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 10:09 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
My HTTP session store a hibernate session which i reconnect and disconnect everytime the filter is invoked.


this is a nice way of doing, Christian also calls it "persistent context" (i've been in the Paris training ;)) ) but be sure to use this only for a unit of work, that is to say 5 screens for example if you need 5 screens to do your job. After this, commit and close().
Of course don't forget to set flushMode.NEVER and commit transaction at each request!. If you don't do this you'll surely have write locks because of unfinished database transaction.

Don't use a hibernate session per "long" user, or you'll soon have a huge session and stale datas.

session.reconnect() just means that the session is ready to execute its job, if it has nothing to do, it will not get a jdbc connection from the pool.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 10:10 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
_persistence_ context ;)

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