-->
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.  [ 8 posts ] 
Author Message
 Post subject: Hibernate in load-balanced scenarios... caching problems?
PostPosted: Fri Apr 16, 2004 2:03 pm 
Newbie

Joined: Fri Nov 21, 2003 4:03 pm
Posts: 3
Location: Boulder, CO
Hi

I am having a strange problem in Hibernate.
I have a web app that is running in a load-balancing environment. So there are two physical servers responding to web requests.
In the application, there is an action that updates a record, the hibernate code is doing:

Code:
            session.save(loan);
            session.flush();
            session.connection().commit();


then there is another action that retrieves the list of objects:

Code:
            allLoans = session.find("from Loan as Loan where status='a'");


but sometimes (due to the load balancing I guess), the application shows the previous version of the data, before the update, that is probably returning cached data, because if I refresh the page, it shows the correct updated information, when the request goes to the other server, and the session.find loads fresh data.

So this looks to me like somehow the data is being cached by one server, because in reality there are two hibernate session running... one on each server.

Is there a way to force Hibernate to return fresh data in a "session.find()" ?

I even added a "session.flush()" before doing the "session.find()" but that didn't help.

Is there some other thing that I am missing here?

Thanks,

_________________
-emilio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 2:07 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If you have no second-level cache for those objects, there is no cache outside of the two Session scopes. You will see changes in one Session as soon as the other Session commits (also check your JDBC connection isolation level).

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 2:08 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
nop it's due to bad management of the hibernate session

take a look at threadlocal session
just search 'open session in view' in the site


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 2:09 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
oh excuse me christian, we have replied at the same time, my 'nop' was for emsuarez of course...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 2:17 pm 
Newbie

Joined: Fri Nov 21, 2003 4:03 pm
Posts: 3
Location: Boulder, CO
Well, actually I am using the ThreadLocalSession and this code gets called after the object (Loan) has been updated.

Code:
    public static void closeHibernateSession() {
        Session session = (Session) threadLocalSession.get();
        AppLog.info("BaseDAO: closing session: " + session);
        threadLocalSession.set(null);
        try {
            if (session != null) session.close();
        } catch (HibernateException e) {
            AppLog.error("Couldn't close the hibernate session" + e);
            e.printStackTrace();
        }
    }


_________________
-emilio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 2:20 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i had exactly the same bug at the beginning i used hibernate (one year ago) and the reason was the management of the session.

You should consider using the servlet filter to ensure that a new sesison is used for each request


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 2:33 pm 
Newbie

Joined: Fri Nov 21, 2003 4:03 pm
Posts: 3
Location: Boulder, CO
Thanks delpouve for your help.

could this also be related that I refactored and now I am instantiating the SessionFactory in a Singleton? So each server has one SessionFactory which returns different sessions to each invocation.

I did review the Open Session in View http://www.hibernate.org/43.html

But this pattern is also creating a SessionFactory and keeping it in the Servlet's context, which works the same as a Singleton in this case.

Or should I create a SessionFactory for each request?

Thanks,

_________________
-emilio


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 16, 2004 2:43 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you only need one sessionFactory per server


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