-->
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: Session Old Values
PostPosted: Wed Nov 17, 2010 8:02 am 
Newbie

Joined: Wed Nov 17, 2010 7:40 am
Posts: 3
Hi

My architecture is Struts 2 with Hibernate.

I have the following in my hibernate.cfg.xml to avoid caching
<property name="cache.use_query_cache">false</property>
<property name="cache.use_second_level_cache">false</property>

I get my sessions from Following getSession() method.

public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

My closeSession() method, what I call after every successful save/update

public static void closeSession() throws HibernateException {
System.out.println("I am i closed the session here?");
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

Whenever I made changes to my object I do the following

Session session = HibernateSessionFactory.getSession();
Transaction tranx = session.beginTransaction();
Try{
//do the transaction
}catch(){
}
tranx.commit();
session.flush();
HibernateSessionFactory.closeSession();


After updating the object (I am calling session.save(object) for update also), when I try to retrieve the object again, I am getting old and new values randomly.

Please help.
Thank you.
Raj


Top
 Profile  
 
 Post subject: Re: Session Old Values
PostPosted: Mon Nov 22, 2010 3:22 am 
Newbie

Joined: Wed Nov 17, 2010 7:40 am
Posts: 3
Any body?


Top
 Profile  
 
 Post subject: Re: Session Old Values
PostPosted: Mon Dec 13, 2010 1:10 pm 
Newbie

Joined: Wed Nov 17, 2010 7:40 am
Posts: 3
Hi,
I found what causing it.

public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}
It was the above ThreadLocal Session instance (in getSession() method)and Tomcat’s threading. Apparently Tomcat uses the thread pool and it will try to reuse the old threads. Some of my sessions are not closed and they are being attached to these threads.

So when old thread is being reused it shows the old value.

Solution:

public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

Since we are using struts 2, I created an interceptor and called the above closeSession() method to close all the sessions.

If your not using struts 2, but you got Servlet, use the Servlet chaining to accomplish above.

Good luck.
Regards
Raj


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