Issue
We use Spring Data JPA and have successfully configured L2 entity and query caches. What still bothers me though is the fact that Hibernate always opens a JDBC connection before checking the L2 cache. This sometimes causes issues (too many open connections) due to a very busy database.
Is there a way to make Hibernate only open a JDBC connection after a L2 cache miss?
Just to clarify, here are the session metrics:
i.StatisticalLoggingSessionEventListener : Session Metrics {
1716707 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
0 nanoseconds spent preparing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
236767 nanoseconds spent performing 1 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
So even though there was absolutely no query performed on the database, a JDBC connection was still opened.
Solution
Thanks to pointers given by Christian Beikov I've found out that the jdbc connection is necessary for the transaction around the operation.
It was the @Transactional
that is enabled for methods in the default Spring Data JPA repository implementation. Overriding one of these methods and leaving the annotation away causes everything to work solely on the cache without any jdbc connection involved.
Answered By - Sander Knopper
Answer Checked By - Clifford M. (JavaFixing Volunteer)