如何检查(调试)JPA 查询是否从缓存或数据库中获取结果
How to check (debug) if JPA query get result from cache or from database
我有简单的 JPA 查询
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();
如何检查这些方法的结果 return 对象列表来自缓存还是直接来自数据库?
在 persistence.xml 我设置了以下参数:
<property name="eclipselink.logging.level.sql" value="FINE"/>
所以在服务器的输出上我可以监控执行的查询(但我不确定 - 如果查询在输出上可见,这意味着查询已发送到数据库或者这意味着查询已发送到 entityManager 和 entityManager决定使用缓存,然后将查询发送到数据库)。
那么我如何区分对象的结果来自:
- 直接从数据库
- 直接从缓存
感谢帮助。
您可以启用 Performance Monitoring:
<property name="eclipselink.profiler" value="PerformanceMonitor"/>
因此,您可以多次执行查询并访问 some cache statistics,例如查询命中缓存的次数:
Integer cacheHits = (Integer)((PerformanceMonitor)session.getProfiler())
.getOperationTimings()
.get(SessionProfiler.CacheHits);
如果您想在更复杂的场景中收集更多详细信息,PerformanceMonitor 已经为您做到了:
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.
我有简单的 JPA 查询
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();
如何检查这些方法的结果 return 对象列表来自缓存还是直接来自数据库?
在 persistence.xml 我设置了以下参数:
<property name="eclipselink.logging.level.sql" value="FINE"/>
所以在服务器的输出上我可以监控执行的查询(但我不确定 - 如果查询在输出上可见,这意味着查询已发送到数据库或者这意味着查询已发送到 entityManager 和 entityManager决定使用缓存,然后将查询发送到数据库)。
那么我如何区分对象的结果来自:
- 直接从数据库
- 直接从缓存
感谢帮助。
您可以启用 Performance Monitoring:
<property name="eclipselink.profiler" value="PerformanceMonitor"/>
因此,您可以多次执行查询并访问 some cache statistics,例如查询命中缓存的次数:
Integer cacheHits = (Integer)((PerformanceMonitor)session.getProfiler())
.getOperationTimings()
.get(SessionProfiler.CacheHits);
如果您想在更复杂的场景中收集更多详细信息,PerformanceMonitor 已经为您做到了:
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.