-->
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.  [ 2 posts ] 
Author Message
 Post subject: very Strange behavior of close() method
PostPosted: Fri Apr 13, 2007 5:04 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: Tue Apr 17, 2007 3:36 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
Take a closer look at code. Not strange really.

HibernateUtil.closeSession() does more than session.close().
>>> session.set(null);

And for the same reason, he opens a new session from sessionFactory on next invocation of getCurrentSession() ;
>>> if (s == null) {
s = sessionFactory.openSession();

.....

-------------------------------------------------------------
Rate the answer if you find it helpful


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