I’m trying to create a cache in a webservice. For this I’ve created a new Stateless Bean to provide this cache to other Stateless beans. This cache is simply a static ConcurrentMap where MyObject is a POJO. The problem is that it seems as there are different cache objects. One for the client beans, and another locally.
-CacheService -CacheServiceBean -getMyObject() -insertMyObject(MyObject) -size() -SomeOtherBean cache = jndiLookup(CacheService) cache.insertMyObject(x) cache.size() -> 1
After this assignment, if I call cache.size from inside the CacheServiceBean, I get 0. Is it even possible to share static singletons through beans? Finally I decided to use a database table, but I’m still thinking about this.
Thanks for your responses.
As far as I remember you cant be certain that the stateless bean is global enough for you to keep data in static fields. There exist several caching frameworks that would help you with this. Maybe memcache?
EDIT: http://java.sun.com/blueprints/qanda/ejb_tier/restrictions.html#static_fields says: