Hibernate : Java Application must be restarted for data to be realoaded
I am programming a JSP/JSF Web application and currently using Hibernate (and MySQL) libraries. When I update data using my running application everything is working fine.
Example : I modify a customer information
Although if I change data manually or add an entry in MySQL manually, the change will not be effective on my application side.
Problem : Each time, I have to reload the application.
Anyone can under开发者_C百科stand my problem and find a rapid solution ?
Are you using second level cache in your web application? If so, your options are:
- Use clustered cache (see above link for options) and remotely invalidate it when your local application makes changes to the database.
- Expose an API (web service or even basic URL) in your web app that you'll have to invoke (manually or via your "local" application) to invalidate the cache. Basically, same as #1 but more work :-) so only use if #1 is not applicable.
If you're not using 2nd level cache but experiencing the issue you've described, that means you're holding on to your Session for way too long. Sessions should generally be short-lived.
Probably Hiberante cache is enabled and needs to be reload the rows/object.
Or you should update/insert data via Hibernate as well.
I'm not sure how you've configured your Hibernate sessions, but some configurations require that you flush your session in addition to closing it (e.g., sessions that are able to be disconnected from the underlying JDBC connection to the DB). So in a comment reply, you said that you end your work with session.close();
-- try doing:
session.flush();
session.close();
instead and see if this fixes what you're seeing.
Otherwise, I'd look to verify that you're really not using a second-level cache... explicitly disable it in your Hibernate configuration, setting hibernate.cache.provider_class
to org.hibernate.cache.NoCacheProvider
.
Try this:
Session session = SessionFactoryUtil.getInstance().openSession();
session.clear();
Alternatively, you can explicitly use a StatelessSession, which has no caching. Details can be found here:
https://www.hibernate.org/hib_docs/v3/api/org/hibernate/StatelessSession.html
Reading the other answers and the comments it's really strange. Can you confirm that you close the Hibernate session at least once every page calls? Are you sure that the other application modifying the database really commits the changes?
精彩评论