I have a struts application that uses Hibernate to access a MYSQL DB. I have a number of pages that make changes to the DB. These changes go through fine, data is update in the 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? I had assumed that as it all went through the hibernate session it would pick up changes? 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()). This will probably degrade performance, but might help you narrow down the problem.