-->
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
PostPosted: Mon May 12, 2008 8:10 am 
Newbie

Joined: Wed Jul 18, 2007 8:15 am
Posts: 9
Hi,

I dont understand "current_session_context_class".The configuration file contains value "thread" for this.Can anybody please explain me the meaning of this in simple terms.

regads,
Ajse


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 8:44 am 
Newbie

Joined: Fri Mar 30, 2007 5:57 am
Posts: 5
Hi,

When you create a hibernate session using any of the sessionFactory.openSession(...) methods the session factory will 'bind' the session to the current context. The default context is 'thread' which means the sesion factory will bind the session to the thread from which openSession(...) is called.

This is useful because you can later call sessionFactory.getCurrentSession() which will return the session that is bound to the currently running thread.

You can use other predefined current_session_context_class values such as 'jta' which will bind the session to the currently running JTA transaction (if you're running in an application server that supports JTA this is really useful). Or you can write your own implementation of org.hibernate.context.CurrentSessionContext and use that implementation to manage the current session context (not really advisable unless you absolutely need to).

Neil


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 8:52 am 
Newbie

Joined: Wed Jul 18, 2007 8:15 am
Posts: 9
Hi,
Thanks for your reply.One thing more.Which thread currently we are talking about?

Suppose a user clicks on three buttons one by one and i have three different DAO classes for that and in each DAO class i have to just call session.getCurrentSession() and proceed?

How does hibernate decide that its a same user which is requesting?Does it utilise HTTPSession for this?Please answer this.

regards,
Ajse


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 9:27 am 
Newbie

Joined: Fri Mar 30, 2007 5:57 am
Posts: 5
I assume you're talking about a web application here in which case each new HTTP request runs in its own new thread (this is not exactly true if your web container uses thread pooling but for the purposes of this explaination each new HTTP request runs in its own new thread).

If each button click in your app creates a new HTTP request then each button click will run in its own separate thread. This means that if you open a session in one request then the session will be bound to the thread for that request. You can reuse the session in that request by calling sessionFactory.getCurrentSession() but you cannot do this for the other requests because they are running in different threads.

Hibernate has no understanding of the HTTP session and doesn't use it for anything - it's just an ORM tool - you need to write all the code to manage your users etc.

The simple way round this is to use the open-session-in-view pattern described in the Hibernate documentation (DON'T put your Hibernate session in your HTTP session - this seems like a good idea at first but is actually VERY BAD).

Neil


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 10:22 am 
Newbie

Joined: Wed Jul 18, 2007 8:15 am
Posts: 9
Let me describe it to see if i have understood it completely.
If there are multiple DAO calls in a single request then session.getCurrentSession() is useful.But if there is only single DAO call per request then session.openSession() is the right way.
Am i correct?

regards,
Ajse


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 10:45 am 
Newbie

Joined: Fri Mar 30, 2007 5:57 am
Posts: 5
Quote:
If there are multiple DAO calls in a single request then session.getCurrentSession() is useful.


Yes that is correct.

Quote:
But if there is only single DAO call per request then session.openSession() is the right way.


Yes and no. But mainly no. Typically, you shouldn't open and close sessions in your DAO classes because they don't know if other DAOs are going to need the same sessions. The best thing to do is open a session for each incoming request (using a filter is the easiest way to do this) and then calling sessionFactory.getCurrentSession() in all your DAOs to get the current session. That way all your DAOs will be easily reusable.

This is the open-session-in-view pattern and is definitely the way forward here. You can find documentation at http://www.hibernate.org/43.html. Opening a session is a very cheap operation (and doesn't necessarily need to obtain a database connection) so opening a session for each incoming request is a minimal performance hit.

Neil


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 10:50 am 
Newbie

Joined: Wed Jul 18, 2007 8:15 am
Posts: 9
Thanks njr28 for your comments.It was really helpful.

Ajse


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.