There’s a piece of code that looks like this. The problem is that during bootup, 2 initialization takes place. (1) Some method does a reflection on ForumRepository & performs a newInstance() purely to invoke #setCacheEngine. (2) Another method following that invokes #start(). I am noticing that the hashCode of the #cache member variable is different sometimes in some weird scenarios. Since only 1 piece of code invokes #setCacheEngine, how can the hashCode change during runtime (I am assuming that a different instance will have a different hashCode). Is there a bug here somewhere ?
public class ForumRepository implements Cacheable
{
private static CacheEngine cache;
private static ForumRepository instance;
public void setCacheEngine(CacheEngine engine) { cache = engine; }
public synchronized static void start()
{
instance = new ForumRepository();
}
public synchronized static void addForum( ... )
{
cache.add( .. );
System.out.println( cache.hashCode() );
// snipped
}
public synchronized static void getForum( ... )
{
... cache.get( .. );
System.out.println( cache.hashCode() );
// snipped
}
}
The whole system is wired up & initialized in the init method of a servlet.
And the init() method looks like this conceptually:
// create an instance of the DefaultCacheEngine
cache = (CacheEngine)Class.forName( "com..DefaultCacheEngine" ).newInstance();
cache.init();
// init the ForumRepository's static member
Object o = Class.forName( "com.jforum....ForumRepository" ).newInstance();
if( o instanceof Cacheable )
((Cacheable)o).setCacheEngine(cache);
// Now start the ForumRepository
ForumRepository.start();
UPDATE I didn’t write this code. It is taken from jforum
UPDATE 2 Solution found. I added a separate comment below describing the cause of the problem. Thanks to everyone.
You’re going to have to give WAY more information than this, but
CacheEngineis probably a mutable data type, and worse, it may even be shared by others. Depending on howCacheEnginedefines itshashCode(), this could very well lead toaForumRepositoryseeing various different hash codes from itscache.It’s perfectly fine for the same object, if it’s mutable, to change its
hashCode()over a period of time, as long as it’s done in a consistent manner (which is another topic altogether).See also
Object.hashCode()— make sure you understand the implications of the contractOn
cachebeingstaticMore information has resurfaced, and we now know that the object in question, while mutable, does not
@Override hashCode(). However, there seems to be a serious issue in design in makingcacheastaticfield ofForumRepositoryclass, with a non-static“setter”setCacheEngine(which looks to be specified byCacheable).This means that there is only incarnation of
cache, no matter how manyForumRepositoryinstances are created! In a way, all instances ofForumRepositoryare “sharing” the samecache!I think it’s important to step back right now and ask these questions:
cacheneed to bestatic? Is this intended?ForumRepositoryhave their owncache?cache?ForumRepositorywill be created?ForumRepositorybe a singleton?setCacheEnginebe called on aForumRepositoryobject?IllegalStateExceptionif it’s called more than once?The best recommendations would depend on the answers to the above questions. The third bullet point is something that is immediately actionable, and would reveal if
setCacheEngineis getting invoked multiple times. Even if they’re only invoked once for eachForumRepositoryinstance, it’s still effectively a multiple “set” in the current state of affairss, since there’s only onecache.A
staticfield with a non-staticsetter is a design decision that needs to be thoroughly reexamined.