2

Stateless EJB's should not have non-final static fields that are used to save state because this does not work in distributed apps with more that one JVM. But if we have a static final Map in EJB to save a few parameters about our distributed applications state is that ok and will the updates on the map be visible to applications running on separate JVMs?

Tomas
  • 1,212
  • 4
  • 15
  • 26

1 Answers1

4

The whole point behind Stateless EJBS is that they are cheap because they don't need to be replicated in a distributed environment.

Therefore any state changes in your static final Map will not be replicated.

You would be better off using a distributed cache such as EhCache, Infinispan or even memcache for this purpose.

Steve C
  • 18,876
  • 5
  • 34
  • 37
  • And if an application is running on one JVM it would work fine? I'm thinking of adding google guava's LoadingCache as a static final field in my stateless bean. As I understand it work as a charm when running on one JVM but it's a good practice solution. Are there any practical problems with this approach? (If we use only one JVM) – Tomas Jul 27 '15 at 15:55