Domanda

Which programming pattern (and product) I should use to create a generic in-memory object cache in EJB 3.0 stateless session bean?

Does use of static member variables or singleton pattern cause any side-effects in clustered environment, if cache is not required to be coherent?

Data to be cached is acquired from JDBC datasources (JPA is not an option) and web services. The amount of data to be cached is few megabytes. The cache must provide very fast reads and therefore I would prefer that the solution should be running inside the JVM that the application is using.

È stato utile?

Soluzione

I guess the only solution is to create a singleton which you store as member variable.

@Stateless
public class MyBean {
    private CacheManager cacheManager;


    @PostConstruct
    public void initialize() {
         cacheManager = CacheManager.getInstance();
    }
}


public class CacheManager {
    private static CacheManager instance;

    public synchronized getInstance() {
       if (instance == null) {
           instance = new CacheManager()
       }
       return instance;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top