Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Configure Query Cacheing in EclipseLink

I have a collection of states, that I want to cache for the life of the application, preferably after it is called for the first time. I'm using EclipseLink as my persistence provider. In my EJB3 entity I have the following code:

@Cache
@NamedQueries({
    @NamedQuery(
        name = "State.findAll",
        query = "SELECT s FROM State s",
        hints = {
                @QueryHint(name=QueryHints.CACHE_USAGE, value=CacheUsage.CheckCacheThenDatabase),
                @QueryHint(name=QueryHints.READ_ONLY, value=HintValues.TRUE)
            }
    )
})

This doesn't seem to do anything though, if I monitor the SQL queries going to MySQL it still does a select each time my Session Bean uses this NamedQuery.

What is the correct way to configure this query so that it is only ever read once from the database, preferably across all sessions?

Edit: I am calling the query like this:

Query query = em.createNamedQuery("State.findAll");
List<State> states = query.getResultList();
like image 279
rustyshelf Avatar asked Feb 25 '09 04:02

rustyshelf


1 Answers

The solutions posted here not worked for me. But i've made it work with:

@Cache
@NamedQueries({@NamedQuery(
      name = "State.findAll",
      query = "SELECT s FROM State s", 
      hints = {
          @QueryHint(name = QueryHints.QUERY_RESULTS_CACHE, value = HintValues.TRUE)
      }
    )})
like image 183
André Luiz Cardoso Avatar answered Oct 11 '22 10:10

André Luiz Cardoso