1

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

David
  • 893
  • 3
  • 10
  • 15
  • 'user loads the page again' is that a reload of that page or a new user (new session)? try closing the browser and open that page again. You should see the new value. – bert Jan 23 '11 at 21:15
  • @bert It's a reload, or a link clicked that opens a different page that also has this component on it also keeps the old value; so something is being cached somewhere I'm guessing. Just tried browser closing; no change ae. – David Jan 23 '11 at 21:24

2 Answers2

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.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • you think you get all the details down yet you never seem to haha.... i use toplink – David Jan 23 '11 at 21:00
  • @David, unfortunately I don't know Toplink so I can't give you more concrete advice. Do you use it directly or via JPA? Hopefully some Toplink expert will come around to help you further... – Péter Török Jan 23 '11 at 21:08
  • No experience with Toplink, but I agree with Peter's suspections about it being in the persistence layer. – jzd Jan 23 '11 at 21:25
  • Thanks Peter! you were right about it being TopLink. I found two solutions. The first being to call a "refresh()" to the entity manager. The second to add ".setHint("toplink.refresh", "true")" to my query. I opted for the second option as then I'll just add it to the areas needed and let TopLink cache any non critical unchanging bits and pieces. – David Jan 23 '11 at 21:44
  • also it's all mentioned here http://weblogs.java.net/blog/guruwons/archive/2006/09/understanding_t.html about halfway down the page it talks about "getting fresh results from db" – David Jan 23 '11 at 21:48
1

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

James
  • 17,965
  • 11
  • 91
  • 146