Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newbie question : Turn off caching

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am using hibernate 3 on jboss 4. I noticed that if I change some data in the db (update some existing data in a column) then hibernate does not load my changes. It loads the stale data from before my update. The only way I can make it load the fresh data is if I bounce the app server.

Is there a setting I can change in the hbm files or in the code to prevent this from happening?

Thanks for any help
Jehan
[ May 08, 2008: Message edited by: Jehan Jaleel ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate has two levels of cache, one that is always on and cannot be turned off. That is the persistence context within one Session instance, and the second level which is turned off by default.

So when you open a Session it creates a PersistenceContext object that has some Maps, it will use those maps to store objects you load. When you close a session, it is gone. Sessions should be short, so that you don't overuse memory and also to lesson the amount of stale data you might have.

If an application outside of Hibernate updates the database, there is not a way for Hibernate to know that that has changed.

There are many things you can do to make hibernate update the cache, or you can use optimistic locking.

Mark
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It loads the stale data from before my update. The only way I can make it load the fresh data is if I bounce the app server.


I'd be very curious to know how long a single Hibernate Session lives in your application? The amount of time between reading data, updating or using data, and then closing your interaction with the database should be short. And you certainly shouldn't have to close your application to get a new Hibernate Session with properly 'syncronized' data.

-Cameron McKenzie
 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys, the issue was that in my code I was not closing the session.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic