4 Replies Latest reply on Oct 5, 2007 7:54 AM by brucespringfield

    Cache not accessible from different methods ?

    jacobeukes

      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.




        • 1. Re: Cache not accessible from different methods ?
          brucespringfield

          I couldn't reproduce your error. But I have POJO Cache deployed as an Mbean service on the JBoss server.

          When I run your test, the object is returned.

          If you are deploying the cache from a jar file within a web application, I'm not sure what the results would be.

          • 2. Re: Cache not accessible from different methods ?
            jacobeukes

            Thank you for the reply.

            Would it be possible to post the configuration file (in which you specify the MBean configuration) ? I have to use JBossCache for a project at work and have been at it for a while now, without any success - the object is just never returned in the second method in the code above. This is getting critical. I have tried to configure and deploy it as an MBean and also tried running it as a standalone application (as part of a test), but I am just not having any luck.

            Thank you for the help - it is much appreciated.

            • 3. Re: Cache not accessible from different methods ?
              manik

              The reason is that in both methods (populateCache and displayCacheContents) you're creating a new cache instance. And if these instances are not configured to be clustered, they won't see state in each other.

              Bruce's case probably worked since when you retrieve the cache from an MBean server rather than create a new one each tme, both cases use the same cache.

              So what you want is probably something like this:

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


              • 4. Re: Cache not accessible from different methods ?
                brucespringfield

                Out of curiosity, which version of POJO Cache are you using?

                "jacobeukes" wrote:

                Would it be possible to post the configuration file (in which you specify the MBean configuration) ?


                Here is a minimal config you could use to deploy POJO Cache as an MBean. It is for POJO Cache version 1.4.1 which is the default POJO Cache that comes with JBoss 4.2.1

                <?xml version="1.0" encoding="UTF-8"?>
                <server>
                 <mbean code="org.jboss.cache.aop.PojoCache"
                 name="jboss.cache:service=PojoCache">
                 <depends>jboss:service=Naming</depends>
                 <depends>jboss:service=TransactionManager</depends>
                 <attribute name="TransactionManagerLookupClass">org.jboss.cache.JBossTransactionManagerLookup</attribute>
                 <attribute name="NodeLockingScheme">OPTIMISTIC</attribute>
                 <attribute name="CacheMode">LOCAL</attribute>
                 <attribute name="LockAcquisitionTimeout">1000</attribute>
                 <attribute name="BuddyReplicationConfig">
                 <config>
                 <buddyReplicationEnabled>false</buddyReplicationEnabled>
                 </config>
                 </attribute>
                 </mbean>
                </server>


                "jacobeukes" wrote:

                I have tried to configure and deploy it as an MBean and also tried running it as a standalone application (as part of a test), but I am just not having any luck.


                The MBean deployment can be quite dependent on getting the right config attributes. If the attibutes or settings aren't quite right, you can get a lot of errors or unexpected behaviour.

                "manik.surtani@jboss.com" wrote:

                Bruce's case probably worked since when you retrieve the cache from an MBean server rather than create a new one each tme, both cases use the same cache.


                Manik is absolutely correct. With an MBean deployment, every access to the cache is accessing the same MBean.