[jboss-user] [JBossCache] - Cache not accessible from different methods ?

jacobeukes do-not-reply at jboss.com
Tue Sep 25 19:13:34 EDT 2007

I will appreciate any help I can get with the following critical issue:

In the code below ...

  | private void populateCache() {
  |   Person person = new Person("Chris","Harris",32);
  |   PojoCache pojoCache = PojoCacheFactory.createCache("META-INF/service.xml", true);
  |   pojoCache.attach("/cachedobjects/person", person);
  |   Person dummyPerson = (Person)pojoCache.find("/cachedobjects/person");
  | 		log.info("Name = " + dummyPerson.getName());
  | }
  | private void displayCacheContents() {
  |   PojoCache pojoCache = PojoCacheFactory.createCache("META-INF/service.xml");
  |   Person person = (Person)pojoCache.find("/cachedobjects/person");
  | 		log.info("Name = " + person.getName());
  | }
  | public static void main(String args[]) {
  |   MyApp app = new MyApp();
  |   myApp.populateCache();
  |   myApp.displayCacheContents();
  | }

... why does all the code in populateCache() execute successfully (it creates an instance of Person, creates an instance of the cache, attaches the object to the cache, finds the object in the cache and then prints out one of its values via a getter), but the code in displayCacheContents() fails  with a NullPointerException at the point where the getter is used (which means an instance of Person is never retrieved from the cache).

I would have thought that the cache would be globally accessible, but it seems that the cache looses its contents as soon as a cache instance goes out of scope. I have created a Singleton that passes back an instance of the PojoCache as a workaround, but this creates other issues, such as that changes on objects do not get propagated to other servers in the cluster.

Thanks for your time and help.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4088622#4088622

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4088622

More information about the jboss-user mailing list