-->
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.  [ 4 posts ] 
Author Message
 Post subject: please explain why i'm getting exception
PostPosted: Fri Apr 13, 2007 2:35 am 
Newbie

Joined: Fri Apr 13, 2007 2:16 am
Posts: 11
Hi,
I am using HibernateUtils for session management : the code is as follows

public class HibernateUtils {
static Logger log = Logger.getLogger(HibernateUtils.class);
public static final SessionFactory sessionFactory;

static {
try {

Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();

} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}

public static final ThreadLocal<Session> session = new ThreadLocal<Session>();

public static Session getCurrentSession() throw HibernateException { Session s = (Session) session.get();
if (s == null) {
s = sessionFactory.openSession();
session.set(s);

System.out.flush();
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null){
System.out.println("session is bieng closed here");
s.close();
System.out.flush();
}
session.set(null);
}
}

In the main application i am doing following things :

Session sess1 = HibernateUtils.getCurrentSession();
sess1.close();
Session sess2 = HibernateUtils.getCurrentSession();

and now i am getting the following exception::
Exception in thread "main" org.hibernate.HibernateException: Not able to obtain connection
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:113)
.....
.........................

and if i change the code as follows:

Session sess1 = HibernateUtils.getCurrentSession();
HibernateUtils.closeSession();
Session sess2 = HibernateUtils.getCurrentSession();
This time i am not getting exception. Why ???????

Sess1.close() and HibernateUtils.closeSession() both doing the same operation means closing the session. then why i am getting different behavior???????


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 14, 2007 10:30 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
In scenario @1:

Code:
Session sess1 = HibernateUtils.getCurrentSession();
sess1.close();
Session sess2 = HibernateUtils.getCurrentSession();


Because your using a ThreadLocal and sess1 and sess2 referr to teh same session instance. Thus, closing sess1 is the same as closing sess2.

In scenario #2:

Code:
Session sess1 = HibernateUtils.getCurrentSession();
HibernateUtils.closeSession();
Session sess2 = HibernateUtils.getCurrentSession();


If you look at what you code is doing in your HibernateUtils class, the answer should be very clear :) Your HibernateUtils.closeSession() nulls out the ThreadLocal value and your HibernateUtils.getCurrentSession() returns a new Session when the ThreadLocal value is null.

This is why it's generally not a good idea to manually close a session when using CMT :)

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject: please explain why i'm getting exception
PostPosted: Mon Apr 16, 2007 5:12 am 
Newbie

Joined: Fri Apr 13, 2007 2:16 am
Posts: 11
Hi Ryan,

Thanks you for replying my post. It is confusing for me what you are trying to explain though it motivated me more and i tried to test the ThreadaLocal instance behavior and now i got the exact explanatiion why i was getting the exception.

Well thanks again for replying my post ( many guys had viewed but you only answered )
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 7:46 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
A simplistic explanation would be that a ThreadLocal variable is kind of like an HttpSession or HttpRequest attribute. If you get an close the Session in one call, the next caller will get a close Hibernate Session.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


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