服务器无法读取来自数据库的更新数据。

3

描述:我们的客户端更新了数据库。然后Hibernate数据库被更新,服务器应该在那个时刻从数据库中读取更新后的值。但是这并没有发生。为了从数据库中读取更新后的值,我必须重新启动服务器。然后我才能看到更新后的值。

发生了什么?

3个回答

3

1
根据您的描述,您可能正在使用Hibernate 2nd level cache(集群或JVM级缓存),当您直接更新数据库时,您显然绕过了Hibernate的API,因此Hibernate没有机会清空缓存以便进行刷新。
如果您想避免不得不重新启动服务器,则需要避免手动更新在手动更新后明确告知Hibernate清空相关部分的缓存,以便数据重新加载。您可以使用SessionFactory级别的各种重载evict()方法来实现这一点。
从Hibernate文档中得知:

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

当然,这需要对应用程序中缓存的内容以及客户端更新的内容有一定的了解。但是必须有人知道这些。

参考资料


0

你是手动更新数据库还是使用Hibernate更新?

如果你是手动更新,根据你的缓存配置,你可能需要重新启动服务器来清除Hibernate缓存。

如果你是使用Hibernate更新数据库,并且无法读取更新后的值,可能与Hibernate的刷新配置有关。

在调用HibernateDaoSupport.getHibernateTemplate().update(transientInstance);后立即刷新,可以调用HibernateDaoSupport.getHibernateTemplate().flush();


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接