Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I thought I was being smart by declaring a bunch of enums in my code so that I could cut down on the clutter of managing several constants that I was going to use for my Coherence cache key names. At first this seemed to work OK but when it came to testing whether the cache was in fact distributed and accessible from multiple services - all hell broke loose.

via GIPHY



I was doing something like this in my code (System.out is just for the purpose of showing the output below)...
 Java
enum KeyEnum { myKey }
...
NamedCache cache = CacheFactory.getCache("myCache");
Object o = cache.get(KeyEnum.myKey);
System.out.println("Initial get: " + o);
if (o == null) {
cache.put(KeyEnum.myKey, "Test String");
System.out.println("Cache key: " + KeyEnum.myKey);
}
System.out.println("Second get: " + cache.get(KeyEnum.myKey));


This always resulted in this output:
 Output
Initial get: null
Cache key: myKey
Second get: Test String


The same output was produced on every service, which should not have been the case as the first service to be invoked would populate the cache and the "Initial get: null" line should only have appeared once.

So I added some more debugging code...
 Java
Set ks = cache.keySet();
Iterator iter = ks.iterator();
System.out.println("------------------------------------------------------------");
while (iter.hasNext()) {
System.out.println(iter.next());
}
System.out.println("------------------------------------------------------------");




After clearing the cache and invoking the first service this additional info was output:
 Output
------------------------------------------------------------
myKey
------------------------------------------------------------


So far so good, however after the second service was invoked, the output changed to this...
 Output
------------------------------------------------------------
myKey
myKey
------------------------------------------------------------


It was an "Aha!" moment. I realised what was going wrong there.

via GIPHY



Coherence requires that everything that is stored in a cache is serialisable and implements equals() and hashCode() methods. This goes for keys as well as values. Looking at the java.lang.Enum documentation actually shows that it meets all this criteria (on the surface). When you dig deeper though, the equals() implementation for Enum looks like this...
 Java
public final boolean equals(Object other) {
return this==other;
}


Doh! That's just an object reference comparison and does not take the (non-transient) enum name into account at all!

Since each enum would have a different memory address across different JVMs (and indeed across different class loaders) this equals() check will always fail inside Coherence. That is why I was seeing two copies of the 'same' key above. As far as Coherence was concerned they were different keys.

It is possible to use something like myKey.name() as the key, but then you may as well use a string constant anyway.

So a lesson learned right there. If you're using non-String keys with Coherence make sure their equals() and hashCode() methods are doing the right thing by taking into account all of the key's non-transient properties and not just doing a simple reference comparison.

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.