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

Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...