Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check (debug) if JPA query get result from cache or from database

I have simple JPA query

  Query query = getEntityManager().createQuery("SELECT pn FROM ProductsNames pn"
            + " WHERE pn.languages = :language"
            + " ORDER BY pn.products.id ASC");
    query.setParameter("language", language);
  return query.getResultList();

How can I check if result of these method return the list of objects from cache or direct from database?

in persistence.xml I have set the following paramter:

 <property name="eclipselink.logging.level.sql" value="FINE"/>

so on output of server I can monitor executed queries (but Im not sure of that - if query are visible on output that`s mean the query was sent to database or that mean the query was sent to entityManager and entityManager decide to use cache and later sent query to database).

So how I can distinguish which result of object are from:

  • direct from database
  • direct from cache

I Will grateful for help.

like image 996
Michał Ziembiński Avatar asked Jul 18 '16 13:07

Michał Ziembiński


People also ask

Does JPA cache query results?

By default, the query will cache 100 query results (see query-results-cache. size); if the same named query with the same arguments is re-executed EclipseLink will skip the database and return the cached results.

Which method in JPA is used to retrieve the entity based on its primary keys?

The EntityManager class defines how our software will interact with the database through JPA entities. Here are some of the methods used by EntityManager : find retrieves an entity by its primary key. createQuery creates a Query instance that can be used to retrieve entities from the database.

Does JPA have cache?

Caching in JPA is required within a transaction or within an extended persistence context to preserve object identity, but JPA does not require that caching be supported across transactions or persistence contexts. JPA 2.0 defines the concept of a shared cache.


1 Answers

You can enable the Performance Monitoring:

<property name="eclipselink.profiler" value="PerformanceMonitor"/>

So, you can execute your query many times you want and access some cache statistics, like the number of times that your query hit the cache:

Integer cacheHits = (Integer)((PerformanceMonitor)session.getProfiler())
    .getOperationTimings()
    .get(SessionProfiler.CacheHits);

If you want collect more details in more complex scenarios, the PerformanceMonitor already do this for you:

The performance monitor will output a dump of cumulative statistics every minute to the EclipseLink log.

The statics contains three sets of information:

  • Info: Statistics that are constant informational data, such as the session name, or time of login.
  • Counter: Statistics that are cumulative counters of total operations, such as cache hits, or query executions.
  • Timer: Statistics that are cumulative measurements of total time (in nano seconds) for a specific type of operation, reading, writing, database operations. Statistics are generally grouped in total and also by query type, query class, and query name.

Counters and timers are generally recorded for the same operations, so the time per operation could also be calculated.

like image 131
Dherik Avatar answered Sep 23 '22 02:09

Dherik