views:

34

answers:

3

Describing ....

our client updates the database. Then the Hibernate database updated, server should read the updated value from database at that moment. But its not happening. To read the updated value from the database, I have to restart the server. Then I see the updated values.

What is happenning?

+3  A: 

Hibernate uses an internal cache to optimize database access.

When you update manually the database, the Hibernate cache isn't updated, so your values doesn't appear immediately. When you restart the server, the cache is emptied, so your new values appear.

You can read more about Hibernate caching and configuration here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/performance.html#performance-cache

Vivien Barousse
A: 

Are you updating database manually, or updating it with hibernate?

If you are doing it manually, depending on your caching configuration, you may need a server restart to clean the hibernate cache.

If you are updating database with hibernate, and if you cant read the updated value, it may related to flushing configuration of hibernate.

To flush immediately after a HibernateDaoSupport.getHibernateTemplate().update(transientInstance); call, you can call HibernateDaoSupport.getHibernateTemplate().flush();

feridcelik
A: 

From your description, you are probably using Hibernate 2nd level cache (a cluster or JVM-level cache) and when you update the database directly, you obviously bypass Hibernate's API and Hibernate does not get the opportunity to empty the cache so that it can get refreshed.

If you want to avoid having to restart your server, either avoid manual updates or explicitly tell Hibernate to empty the relevant parts of the cache after a manual update so that data will get reloaded. You can do this using the various overloaded evict() methods at the SessionFactory level.

From the Hibernate documentation:

19.3. Managing the caches

...

For the second-level cache, there are methods defined on SessionFactory for evicting the cached state of an instance, entire class, collection instance or entire collection role.

sessionFactory.evict(Cat.class, catId); //evict a particular Cat
sessionFactory.evict(Cat.class);  //evict all Cats
sessionFactory.evictCollection("Cat.kittens", catId); //evict a particular collection of kittens
sessionFactory.evictCollection("Cat.kittens"); //evict all kitten collections

Of course, this will require some knowledge of what is cached in your application and of what gets updated by your client. But someone must know this.

References

Pascal Thivent