简体   繁体   English

Hibernate 未反映更改

[英]Hibernate not reflecting changes

I have a struts application that uses Hibernate to access a MYSQL DB.我有一个使用 Hibernate 访问 MYSQL 数据库的 struts 应用程序。 I have a number of pages that make changes to the DB.我有许多对数据库进行更改的页面。 These changes go through fine, data is update in the DB.这些更改 go 通过精细,数据在 DB 中更新。 However when browsing to the page that should show this updated information its often not there, and even after a few page refreshes it still isn't there.但是,当浏览到应该显示此更新信息的页面时,它通常不存在,即使在几次页面刷新后它仍然不存在。 Eventually it will turn up.最终它会出现。 I'm assuming this has something to do with hibernate caching data, but how can I ensure that data is up to date?我假设这与 hibernate 缓存数据有关,但是如何确保数据是最新的? I had assumed that as it all went through the hibernate session it would pick up changes?我以为这一切都通过 hibernate session 它会发生变化? The code i'm using to do the update is:我用来进行更新的代码是:

    hSession = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tx = hSession.getTransaction();
    tx.begin();
    hSession.update(user) ;

Then to pull that user out again:然后再次将该用户拉出:

org.hibernate.Session hSession = HibernateUtil.getSessionFactory()
.getCurrentSession();
 Transaction tx = hSession.beginTransaction();
 User u= (User) hSession.load(User.class, userID);

Too little information to really give an answer.信息太少,无法真正给出答案。 But some points to check:但有几点需要检查:

  • You are using transactions.您正在使用事务。 Are you properly committing them?你是否正确地提交了它们? Maybe at some point your code cannot see changes, because the are not yet commited (or because the reading code is in another transaction which uses previous state).也许在某些时候您的代码看不到更改,因为尚未提交(或者因为读取代码在另一个使用先前状态的事务中)。

  • The cache might also be a problem.缓存也可能是个问题。 To check, you could explicitly flush the cache after each change to the DB (Session.flush()).要检查,您可以在每次更改数据库 (Session.flush()) 后显式刷新缓存。 This will probably degrade performance, but might help you narrow down the problem.这可能会降低性能,但可能会帮助您缩小问题范围。

It is probably a result of thesecond level (session factory) hibernate cache .这可能是二级(会话工厂) hibernate 缓存的结果。

Caching should be transparent and work fine with the code you gave, but problems occur usually when:缓存应该是透明的,并且可以与您提供的代码一起正常工作,但通常在以下情况下会出现问题:

  1. You are running a cluster of machines and do not have a way for the caches to invalidate each other configured您正在运行一个机器集群,并且没有办法让配置的缓存相互失效
  2. You are updating the database outside of hibernate.您正在更新 hibernate 之外的数据库。

The easiest way to determine if it is a second-level cache problem is to completely disable the cache in your hibernate config.确定它是否是二级缓存问题的最简单方法是完全禁用 hibernate 配置中的缓存。 If it is the cache, you can configure a cluster to know about each other so they can manage the cache automatically, or if it is a problem with outside-hibernate updates you can manually invalidate cache items with the hibernate api如果是缓存,您可以将集群配置为相互了解,以便他们可以自动管理缓存,或者如果是外部休眠更新的问题,您可以使用hibernate api手动使缓存项无效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM