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.


I was doing something like this in my code (System.out is just for the purpose of showing the output below)...
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:
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...
Set ks = cache.keySet();
Iterator iter = ks.iterator();
while (iter.hasNext()) {

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

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

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


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...
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.


Have comments or feedback on what I wrote? Please share them below! Found this useful? Consider sending me a small tip.
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

Oracle WebLogic Maven Plugin wsgen goal not respecting the Maven Compiler Plugin version

How to fix Google Cloud SDK dev server error - No module named ipaddr

Adorable but totally metal - Metal Earth 3D Guardians of the Galaxy Groot model kit

Riverside Expressway Cam shut down permanently

Inserting Google DFP ads with Backbone, Underscore and jQuery

How to resolve the domain is already mapped to a project error in Google App Engine

A quick look at the Nyko Super MiniBoss wireless controllers for the SNES mini

Loading and displaying a collection from bootstrapped data in Backbone.js

Add this handy function to your Bash profile file to display the compiled JDK version for a .class file

How does PCBWay stack up as a low budget PCB fab

Recent Galleries

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Tiny Arcade revision 6 kit assembly and decal application

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

Blogs and Friends

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

Blog Activity

Blog Activity