3

I am developping an application in Java EE, I would like to implement a cache using a @Singleton EJB. This caches, referencial data, so I only need retrieved once from the DB and then store it in memory.

I would like to know from an implementation point of view if this is correct, using a @Singleton EJB ? or could you recommend me another approach ? and also if this is correct from an OOP perspective ?

And the @Singleton EJB is for read-only, is there any concurrency issues I could encounter ?

Regards,

2
  • 1
    Done all the time in J2EE applications and is recommended for read only data. Only catch is, if Db changes, then either you need to reload somehow or it will reflect when u restart app. Commented Jul 22, 2016 at 7:14
  • Since you can reload, update, refresh with an observer, it's not really a catch. docs.oracle.com/javaee/6/tutorial/doc/gkhic.html Commented Jul 25, 2016 at 17:35

2 Answers 2

1

The approach is ok, but the drawback is that is it not simple to enhance the solution later - no nice interface. But possible with every JavaEE server without any migration effort as it is standard JavaEE.

Another solution depends a bit on the server you use.

WildFly (community): you might use the internal infinispan subsystem and use is in a HashMap manner. You can simple use it local to start with and change the configuration to clustered (replicated or distributes) if the cache grow and you need more memory to cache it.

JBoss EAP (Enterprise Product): Here you can't use the Infinispan subsystem, technical it is possible but it is not supported. You need to use the additional JBossDataGrid (JDG) which is based on infinispan. Here you have more options, same as above use the cache in the same JVM local or dist/repl. Or on a different instance with remote access to the cache - often fast enough but you have one remote access - but the JVM is complete separated from the server and can be started maintained different. Also the server and cache did not affect each others memory.

For other vendors you can use the JDG approach (or Infinispan as OpenSource) also.

Sign up to request clarification or add additional context in comments.

Comments

1

As a quick and easy solution Singleton EJB can help, especially if catalogs whose values do not change.
Just consider your EJB Singleton establish the following:

  • Concurrency Management by Container
  • All methods for establishing LockType.READ accessed concurrently by any arbitrary number of clients

For example:

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Singleton
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class InitializationBean {

    @PostConstruct
    public void initialize() {
        // load data
    }

    @Lock(LockType.READ)
    public List<String> getCatalog01() {
        return null;

    }

    @Lock(LockType.READ)
    public List<String> getCatalog02() {
        return null;

    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.