简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM