-->
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.  [ 7 posts ] 
Author Message
 Post subject: current_session_context_class settings
PostPosted: Wed Dec 24, 2008 2:36 am 
Newbie

Joined: Wed Dec 24, 2008 2:18 am
Posts: 5
I am using Java Web Server 7 and Oracle 10g for my application. I have set hibernate setting current_session_context_class to thread. I was expecting hibernate to open a new session for each http request and close the session at the end of the request. The behaviour I observed is that:

1. Hibernate is using the same session for multiple requests from the same user.
2. Java web server is using the same thread for multiple requests from the same user.

It seems that since Java web server is using the same thread to service multiple requests from the same user. May be this is because java web server is using the same thread to service multiple requests over the same connection. And since hibernate session is bound to a thread, hibernate is using the same session to service multiple requests.

This is contrary to the expectation that each request will have its own unique session. Please provide your thoughts and resolution of this problem.

_________________
Let us work this out.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 25, 2008 12:57 pm 
Newbie

Joined: Wed Dec 24, 2008 2:18 am
Posts: 5
Any comments or help from your experience. I am stuck in resolving this issue. Hibernate is using the same session to service multiple requests from the same user and that is breaking the application design.

_________________
Let us work this out.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2008 12:56 am 
Beginner
Beginner

Joined: Wed Nov 26, 2008 1:02 am
Posts: 22
hi,

how u got the session ,i mean using SessionFactory.getCurrentSession(),or openSession().


regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 29, 2008 12:38 am 
Newbie

Joined: Wed Dec 24, 2008 2:18 am
Posts: 5
I got the session using sessionFactory.getCurrentSession().

_________________
Let us work this out.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 11:48 am 
Newbie

Joined: Wed Dec 24, 2008 2:18 am
Posts: 5
I got the issue resolved. The trick is unbind the session from the thread at the end of request.
org.hibernate.context.ThreadLocalSessionContext.unbind(HibernateUtil.getSessionFactory());

This will unbind the current session from the thread. The session is linked as a thread local variable when current_session_context_class property is set to thread. Thread local variables are not cleaned at the end of http request. We need to explicitly clean those.

Hope this will help someone facing similar issue.

- Pawinder

_________________
Let us work this out.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 6:12 am 
Newbie

Joined: Fri Jan 09, 2009 4:31 am
Posts: 9
I use hibernate in servlet environment about 2 years, and i never unbind hibernate session. In "Java persistence with Hibernate" says:
"All the data-access code that calls getCurrentSession() on the global shared
SessionFactory gets access to the same current Session—if it’s called in the
same thread. The unit of work completes when the Transaction is committed (or rolled back). Hibernate also flushes and closes the current Session and its persistence context if you commit or roll back the transaction. The implication here is that a call to getCurrentSession() after commit or rollback produces a new Session and a fresh persistence context."


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 10, 2009 5:55 am 
Newbie

Joined: Wed Dec 24, 2008 2:18 am
Posts: 5
That's exactly what I expected.

But I have experienced that Hibernate does not close the session after a transaction is committed or rolled back. If I begin antoher transaction after committing one transaction, hibernate uses same session. An explicit session.close() is required to close the session. I am using Hibernate 3.

Another issue with this approach is that all developers might not commit() or rollnback() the transaction after use. E.g. if you are querying the database and not doing any dml operation, you might not issue a commit() or rollback() after querying.

Hibernate binds the session with thread. Threads are reused by webserver. Thus if the session bound to the thread is not closed at the end of request, the session will be reused by next request serviced by the same thread.

Thus, it is safe and reliable to unbind the session at the end of every request. I am using hibernate 3 in my project.

_________________
Let us work this out.


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