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

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

Coherence and weblogic.xml in different types of J2EE web app deployments

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 1 - introduction and case disassembly

jPhotoFrame updated to version 0.3.1 with an image rotation correction utility

iOS 11 pre-GM mini review before it gets revealed next week

Why you should never use Java enums as keys with Oracle Coherence caches

Hacking the VideoPak 7inch promotional brochure to work as a digital photo frame

Recent Galleries

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity