problem with updating application with new data from database

别说谁变了你拦得住时间么 提交于 2019-12-11 12:45:38

问题


I have a web application that pulls data from the database then displays it. Now the problem I'm having is that every hour I want to do updates on the data in the database; this is done by a separate app, this all works fine. However, once this has happened and the user refreshes their web page, the new data isn't being displayed.

I hope this makes sense.

I'll provide some code from the app so that you see it better.

My web app is written in netbeans using java and I have a persistence unit that is mapping classes to tables in the database. I'm using wicket to add my components to my html.

// Java Wicket code

ApplicationSettings apset = new ApplicationSettingsDAO().getApplicationSettings();    
this.add(new Label("lblGameTime", "" + apset.getGameTimeDays()));

// Wicket html code

Date : day <span wicket:id="lblGameTime">generated date</span>

// DAO application settings which gets the settings object from the db and returns it to the Java Wicket code above

public ApplicationSettings getApplicationSettings() {

    ApplicationSettings settings;

    try {
     Query q = manager.createQuery("select s from ApplicationSettings s where s.setting = :setting");
     q.setParameter("setting", "setting");
     settings = (ApplicationSettings) q.getSingleResult();
  } catch (NoResultException e) {
     ApplicationSettings newSetting = new ApplicationSettings();
     return newSetting;
  }
     return settings;

}

//ApplicationSettings is just the class.

//Hourly code ran in another app that works and does update db

Query query6 = manager.createQuery("select s from ApplicationSettings s where s.setting = :setting");
    query6.setParameter("setting", "setting");
    ApplicationSettings apset = (ApplicationSettings) query6.getSingleResult();
    apset.setGameTimeDays(apset.getGameTimeDays() + 1);
    save(apset);

Now when the user loads the page again I'd expect that the gameTimeDays would be correct as the page would get the data from the db again because it'd call the java wicket code above. BUT the value does not change. The value that shows up on the page is the old stale value before the update. No matter how many updates take place this value is still the old value. HOWEVER if I do a redeploy or restart the module in the tomcat server, the value is then correct for that moment in time.

What am I missing, this is a tiny problem that the answer seems to be alluding me.

The value seems to be stored in memory till the server is redeployed or something. I dont know alot about the inner workings of java to know about this.

Ok I hope I provided enough info for someone to help me with my dilemma.

Thanks for any help.

EDIT: persistence library is TopLink Essentials


回答1:


Sounds like your data is being cached by the persistence mechanism you use (about which you don't disclose details). This is not a feature of Java, but that of specific persistence frameworks like e.g. Hibernate.

If you let us know more about your persistence provider, we may be able to give more concrete advice.




回答2:


You can disable the shared cache in TopLink using the persistence.xml property,

Or disable it per Entity, or use refreshing or cache invalidation.

EclipseLink provider more caching options and implements the JPA 2.0 caching API.

http://wiki.eclipse.org/EclipseLink/FAQ/How_to_disable_the_shared_cache%3F



来源:https://stackoverflow.com/questions/4776412/problem-with-updating-application-with-new-data-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!