描述:我们的客户端更新了数据库。然后Hibernate数据库被更新,服务器应该在那个时刻从数据库中读取更新后的值。但是这并没有发生。为了从数据库中读取更新后的值,我必须重新启动服务器。然后我才能看到更新后的值。
发生了什么?
SessionFactory
级别的各种重载evict()
方法来实现这一点。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
当然,这需要对应用程序中缓存的内容以及客户端更新的内容有一定的了解。但是必须有人知道这些。
你是手动更新数据库还是使用Hibernate更新?
如果你是手动更新,根据你的缓存配置,你可能需要重新启动服务器来清除Hibernate缓存。
如果你是使用Hibernate更新数据库,并且无法读取更新后的值,可能与Hibernate的刷新配置有关。
在调用HibernateDaoSupport.getHibernateTemplate().update(transientInstance);
后立即刷新,可以调用HibernateDaoSupport.getHibernateTemplate().flush();