-->
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 about session behaviour
PostPosted: Sat Oct 28, 2006 12:33 am 
Newbie

Joined: Sat Oct 21, 2006 10:51 pm
Posts: 7
I have a JSF web app with Hibernate 3.1. The session-per-request pattern is set up by having "current_session_context_class" set to "thread" in hibernate.cfg.xml. The database connections are obtained from a connection pool configured in the application server (Sun Application Server PE 8.2).

I can do r/w operations obtaining the session from HibernateUtil.getCurrentSession(). However, if after commit I do a Hibernate.getCurrentSession(), the operation (newSession == lastSession), where lastSession is a reference to the previous session, returns false. I can conclude that the new session is different from the new session.

From Hibernate docs I've understood that, given the thread-bound session, the second call to getCurrentSession() should return the same session. The observed behaviour is wrong or do I have a problem in the hibernate config? Using the newly obtaned session will fall into the session-per-operation anti-pattern?

I'll appreciate your orientation.
Thank you in advance.
Antonio-


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 28, 2006 12:55 am 
Newbie

Joined: Sat Oct 21, 2006 10:51 pm
Posts: 7
To be more precise:
    - Calling HibernateUtil.getCurrentSession() many times before commit() returns the same session.
    - Calling HibernateUtil.getCurrentSession() after commit() returns a new session and lastSession.closed == true, of course, unless I'm incorrectly comparing both session objects.


I presume this is the correct behaviour.

Thank you.


Top
 Profile  
 
 Post subject: Re: Question about session behaviour
PostPosted: Sat Oct 28, 2006 2:36 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
unixlibre wrote:
From Hibernate docs I've understood that, given the thread-bound session, the second call to getCurrentSession() should return the same session. The observed behaviour is wrong or do I have a problem in the hibernate config? Using the newly obtaned session will fall into the session-per-operation anti-pattern?


Im not sure where you saw the above, this is also from the hibernate docs
Quote:
A Session begins when it is first needed, when the first call to getCurrentSession() is made. It is then bound by Hibernate to the current thread. When the transaction ends, either through commit or rollback, Hibernate automatically unbinds the Session from the thread and closes it for you. If you call getCurrentSession() again, you get a new Session and can start a new unit of work. This thread-bound programming model is the most popular way of using Hibernate, as it allows flexible layering of your code (transaction demarcation code can be separated from data access code, we'll do this later in this tutorial).


I have not tried this, but can you set the auto_close_session value to be false, so that the session is not closed at the end of a commit/rollback? That way, you could have the session maintained as a session-per-request pattern, but its your responsibility to close it after the thread has completed?


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.