3 Replies Latest reply on Jan 6, 2004 5:48 AM by Horia Muntean

    Caching Objects / POJOs in jbosscache

    msakib Newbie

       

      "msakib" wrote:
      "msakib" wrote:
      Hello,

      The jbosscache unit tests have a rich set of scenarios, but one big issue with these tests is that it only putting strings in the cache.

      In a normal business application scenario, we will generally be putting objects (e.g. HashMaps, POJOs etc.).

      I modified one of the simple unit tests (testPutRollback2() function in TxUnitTestCase.java in the jbosscache samples) to put a HashMap in the cache and modify in a transaction. The cache does not seem to rollback the value in the HashMap, if the transaction is rolledback. The modified function is pasted below.

      I am following the general steps that mostly everybody will require when using the cache. i.e.
      1) Start transaction
      2) Get object out of the cache
      3) call setters on the object
      4) set the object on the cache
      5) continue business processing
      6) further processing may decide to rollback cache
      7) rollback or commit the cache

      If jbosscache does not support this basic scenario then it will be very difficult for us ( or i guess anybody) to use it in a real-world application where we want to cache objects.

      May be I am missing some steps. Any help on this will be appreciated.

      We are looking at using a caching solution for a distributed, high performance, high volume financial applications. jbosscache features seem good, but we came across the above issue.


      Test Function code:
      -----------------------------------------------------------------------------

      // modified To test with a HashMap
      public void testPutRollback2() {
      try {
      //Add a HashMap
      HashMap testMap = new HashMap();
      testMap.put("TESTMAP","ORIGMAPVALUE");
      cache.put("/bela/ban", "map", testMap);

      cache.put("/bela/ban", "name", "Bela"); // /bela/ban needs to exist
      tx.begin();
      //Get the HashMap modify it and put it back
      HashMap testMap2= (HashMap)cache.get("/bela/ban", "map");
      testMap2.put("TESTMAP","NEWMAPVALUE");
      cache.put("/bela/ban", "map", testMap2);

      cache.put("/bela/ban", "name", "Michelle");
      assertEquals("Michelle", cache.get("/bela/ban", "name"));
      tx.rollback();
      assertEquals("Bela", cache.get("/bela/ban", "name"));

      //Get the HashMap Value and Assert it
      HashMap retMap = (HashMap)cache.get("/bela/ban", "map");
      System.out.println(" Return Map Value="+"Return Value="+(String)retMap.get("TESTMAP"));
      assertEquals("ORIGMAPVALUE", (String)retMap.get("TESTMAP"));
      }
      catch(Throwable t) {
      t.printStackTrace();
      fail(t.toString());
      }
      }

      -----------------------------------------------------------------------------

      Again any kind of help will be appreciated.

      Best Regards,
      Sakib


        • 1. Re: Caching Objects / POJOs in jbosscache
          Horia Muntean Newbie

           

          "seven" wrote:
          "seven" wrote:
          Did you use TreeCache or TreeCacheAOP? I think only TreeCacheAOP supports Collections (see http://www.jboss.org/developers/projects/jboss/cache/TreeCacheAop.html).

          Regards,
          Horia


          • 2. Re: Caching Objects / POJOs in jbosscache
            msakib Newbie

             

            "msakib" wrote:
            "msakib" wrote:
            I used TreeCache. I just used a HashMap for the test, basically I wanted to test whether I could store any simple objects( POJOs) in the cache. Which does not seem to be possible.

            If TreeCache only works with Strings, it is very limiting.


            • 3. Re: Caching Objects / POJOs in jbosscache
              Horia Muntean Newbie

               

              "seven" wrote:
              "seven" wrote:
              Try to get the map, clone it and put it back:

              //Get the HashMap, clone it, modify it and put it back
              HashMap testMap2= (HashMap)cache.get("/bela/ban", "map");
              HashMap clone = (HashMap)testMap2.clone();
              clone.put("TESTMAP","NEWMAPVALUE");
              cache.put("/bela/ban", "map", clone);

              This behaviour is normal, since the objects are not "advised" and TreeCache is not AOP aware, so the framework is unable to intercept the changes you are making using direct references to the target object.

              Regards,
              Horia

              P.S. This is a bsh snippet I used to test this case:

              tx.begin();
              map = tree.get("/bela/ban", "map");
              System.out.println(tree.get("/bela/ban","map"));
              //should print original value

              nmap = map.clone();
              nmap.put("TESTMAP","value5");
              tree.put("/bela/ban","map",nmap);
              System.out.println(tree.get("/bela/ban","map"));
              //should print last value

              tx.rollback();
              System.out.println(tree.get("/bela/ban","map"));
              //should print original value