3. Prepared SQL Cache

Prepared SQL Cache caches SQL statements corresponding to JPQL queries. If a query is executed more than once in the same or different persistence contexts, the SQL statement generated during the first execution is cached and executed directly for subsequent execution. Direct execution of SQL offers significant performance gain as it saves the cost of parsing query string and, more importantly, populating the query expression tree during every execution. Relative performance gain becomes higher as the complexity of forming a SQL query from a JPQL string increases. For example, a JPQL query Q1 that involves multiple joins across tables takes more computation to translate into a SQL statement than a JPQL query Q2 to select by simple primary key identifier. Correspondingly, repeated execution of Q1 will gain more performance advantage than Q2 with Prepared SQL Cache.

Prepared SQL Cache is configured by the openjpa.jdbc.QuerySQLCache configuration property. This property accepts a plugin string (see Section 4, “ Plugin Configuration ”) with value of true or false. The default is true. The execution statistics of the cached queries can be optionally collected as

  <property name="openjpa.jdbc.QuerySQLCache" value="true(EnableStatistics=true)">

The QueryStatistics can be accessed via PreparedQueryCache.getStatistics().

Table 10.3.  Pre-defined aliases

true org.apache.openjpa.util.CacheMap The default option. Uses a CacheMap to store SQL string. CacheMap maintains a fixed number of cache entries, and an optional soft reference map for entries that are moved out of the LRU space. So, for applications that have a monotonically increasing number of distinct queries, this option can be used to ensure that a fixed amount of memory is used by the cache.
falsenone Disables the SQL cache.

Following salient points to be noted regarding usage of Prepared Query Cache.

Several mechanisms are available to the application to bypass SQL caching for a JPQL query.