How to chage the Oracle Coherence backing map

239 views Asked by At

I am new to Oracle Coherence, I've tried out the hello-world example and worked fine, but I noticed behavior that I don't want, which is when I store an object to the cache, and tried to get this object and update it, this update didn't propagate to the cache, am not sure but i think this is due to the usage of com.tangosol.util.SafeHashMap.

cache config: I am using the default coherence.jar configuration files.

my code:

    NamedCache cache = CacheFactory.getCache("test");

    HashMap<Integer, LinkedHashMap<Integer, String>> m = new HashMap<Integer,   LinkedHashMap<Integer, String>>();

    Map<Integer, String> l = new LinkedHashMap<Integer, String>();
    l.put(1, "val-1");
    l.put(2, "val-2");

    Map<Integer, String> l2 = new LinkedHashMap<Integer, String>();
    l2.put(3, "val-3");
    l2.put(4, "val-4");

    m.put(1, (LinkedHashMap<Integer, String>) l);
    m.put(2, (LinkedHashMap<Integer, String>) l2);


    cache.put(1, l);
    cache.put(2, l2);


    System.out.println(cache.get(1));
    System.out.println(cache.get(2));
    // output is:
    //{1=val-1, 2=val-2}
    //{3=val-3, 4=val-4} 

    ((LinkedHashMap<Integer, String>)cache.get(1)).remove(1);

    System.out.println("====== AFTER CHANGE ========");

    System.out.println(cache.get(1));
    System.out.println(cache.get(2));
    // output STILL :
    //{1=val-1, 2=val-2}
    //{3=val-3, 4=val-4} 

output:

   {1=val-1, 2=val-2}
    2014-11-16 12:51:53.214/6.333 Oracle Coherence GE <D4> (thread=ShutdownHook,      member=1): ShutdownHook: stopping cluster node
   {3=val-3, 4=val-4}
   ====== AFTER CHANGE ========
   {1=val-1, 2=val-2}
   {3=val-3, 4=val-4}

The above behavior seems to me to be like HttpSession object.

My Question is, how to change this behavior and when I do change in the object, this change propagate to the cache?

1

There are 1 answers

2
Peter Lawrey On

When you get an object from the cache, you are working with a copy. If you want to also alter the cache, you need to put the new/altered object back into the cache.