-->
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.  [ 14 posts ] 
Author Message
 Post subject: Hibernate caching
PostPosted: Wed Apr 02, 2008 2:29 pm 
Newbie

Joined: Wed Apr 02, 2008 2:25 pm
Posts: 6
Hi,

I'm having a problem with my webapp that I think is caused by Hibernate. In a nutshell, when I update a database table outside the webapp, I don't see the updated data when I refresh the page. The data remains stale until I restart the whole webapp.

I have caching disabled:

Code:
    <property name="hibernate.cache.use_second_level_cache">false</property>
    <property name="hibernate.cache.use_query_cache">false</property>


I verified that Hibernate does actually query the database by printing out Hibernate session stats:

Quote:
... queries executed to database=5,query cache puts=0,query cache hits=0, ...


Finally, if I use a plain JDBC connection without Hibernate for the same query, the problem disappears... I would appreciate any suggestions and hope someone can shed some light on this problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 03, 2008 2:32 am 
Regular
Regular

Joined: Mon Aug 06, 2007 10:49 am
Posts: 67
Location: Banska Bystrica, Slovakia
how do u manage sessions? dont u have opened session for whole life of app?

and look in log for sql to see if hibernate does the query u expected


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 03, 2008 3:44 pm 
Newbie

Joined: Wed Apr 02, 2008 2:25 pm
Posts: 6
My sessions are opened and closed per each request. I have show sql set to true and I can see from the logs that hibernate does do the expected query for every request... but still comes back with stale data.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 04, 2008 6:48 pm 
Newbie

Joined: Fri Mar 21, 2008 2:20 pm
Posts: 10
Location: London
Are you sure the web page isn't being cached? :)

_________________
http:// www.techfoundry.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 1:21 pm 
Newbie

Joined: Wed Apr 02, 2008 2:25 pm
Posts: 6
That was one of the first things I checked. :) Also, as I mentioned, I have show_sql enabled and I can see that the queries are being made.

Any other suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 3:03 pm 
Newbie

Joined: Fri Mar 21, 2008 2:20 pm
Posts: 10
Location: London
All out of sensible suggestions I'm afraid :)

I've experienced similar myself when using JPA - the caching being caused by the EntityManager holding the object graph in memory, in the case where you keep the EntityManager alive across multiple tx's.

If you then update the database in another thread using another EntityManager or via JDBC or whatever, the EntityManager cannot know about the update, and will continue to provide the cached stale objects (in response to queries that match the cached objects).

One thing you could try is using your debugger to look at the instance variables the Hibernate holds, just to make sure that after you close the session, the objects really have gone from memory and are not hanging around somewhere... just an idea. I guess you'd have to do this outside your web app.

So the update outside of the web app - are you sure this is committing the transaction that does the update to the database... if you verify the update by doing an insert then a select within the same transaction, it's going to look like the update works, but the web app won't see the update (assuming no caching) until the transaction is committed.

I do remember some similar problem where somebody had a delayed commit turned on in their database...

That's about the end of my guesses :)

Andy.

_________________
http:// www.techfoundry.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 5:18 pm 
Newbie

Joined: Tue Jan 22, 2008 9:53 am
Posts: 18
hi,

Have you tried to call session.clear(); before making your queries ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 5:55 pm 
Newbie

Joined: Wed Apr 02, 2008 2:25 pm
Posts: 6
@Andy

Thanks for the tips. I also believe that it's some caching issue. What puzzles me though, is that I have caching disabled. Also I'm not using JPA.

Re committing the tx, I verified it by querying the db directly, so the table gets updated for sure. + I tested the webapp using a plain JDBC connection which solves the problem - each request returns updated data.

@thiagodf

session.clear() didn't make a difference unfortunately...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 08, 2008 4:43 pm 
Newbie

Joined: Tue Apr 08, 2008 4:38 pm
Posts: 1
Location: Brasil, São Paulo
Try start a tx even before perform a select using hibernate...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 09, 2008 3:03 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
If I had to much money I'd bet you are using an old session/transaction.

Try the following to hunt down the problem:

1. enable cache logging. to see that really no 2nd level cache is used.
2.enable caching to check that the logging is working.
3. disable caching again.
4. when getting the new Session print it out to log/console and compare with the value from the previous request, to make sure you get a new one every time.
5. before the select in question use session.getStatistics to ensure the objects you are about to select aren't already in the session.

Jens

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 5:00 pm 
Newbie

Joined: Wed Apr 02, 2008 2:25 pm
Posts: 6
Hi Jens,

Thanks for the pointers. I've verified through the logs that caching is disabled:

Code:
org.hibernate.cfg.SettingsFactory Second-level cache: disabled
org.hibernate.cfg.SettingsFactory Query cache: disabled
org.hibernate.cfg.SettingsFactory Cache provider: org.hibernate.cache.NoCacheProvider


And the statistics confirm this also. Main things to note is that sessions are opened and closed properly, a database query is made on each request, and there are no cache puts/hits/misses.

Code:
Statistics[
start time=1207859958519
sessions opened=2
sessions closed=2
transactions=0
successful transactions=0
optimistic lock failures=0
flushes=0
connections obtained=5
statements prepared=38
statements closed=38
second level cache puts=0
second level cache hits=0
second level cache misses=0
entities loaded=273
entities updated=0
entities inserted=0
entities deleted=0
entities fetched=3
collections loaded=30
collections updated=0
collections removed=0
collections recreated=0
collections fetched=30
queries executed to database=5
query cache puts=0
query cache hits=0
query cache misses=0
max query time=147]


I still haven't figured out what's causing this problem, so any other suggestions would be most welcome.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 10, 2008 5:09 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
hibernate2008 wrote:
I still haven't figured out what's causing this problem, so any other suggestions would be most welcome.



Explain the sequence of the things and explain how it works with jdbc. I'd like to know how you get connections, how the sql is executed in both cases, what the db is, what is the full hibernate configuration file and all the log you see.



Farzad-


Top
 Profile  
 
 Post subject: Re: Hibernate caching
PostPosted: Wed Apr 14, 2010 8:09 am 
Newbie

Joined: Fri Mar 12, 2010 8:51 am
Posts: 2
try using
(UrObject) session.get(UrObject.class, idOfUrObject);
instead of
(UrObject) session.load(UrObject.class, idOfUrObject);

greets

Tim


Top
 Profile  
 
 Post subject: Re: Hibernate caching
PostPosted: Wed Apr 14, 2010 8:43 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Suggestion: Log all your sql-statements (and the regarding result-sets) with p6spy.
As first step plaase analyze the result-sets to see if it is the database which returns you stale data (in this case it may a problem of isolation level) or if it is hibernate returning stale data (hibernate internally within a persistent context simulates a repeatable read isolation behavior, unless you call clear)


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