liveBook · Manning

Jdbc profiling

As explained in Troubleshooting Java: Read, debug, and optimize JVM applications

Profiling that intercepts database queries sent through the Java Database Connectivity API.

JDBC profiling intercepts database activity from a Java application. It can reveal the SQL a persistence framework generates or show whether database connections are opened and closed correctly. section 7.3.3 section 8.1

Use the observations to narrow down the problem:

  • Inspect intercepted SQL for unexpected joins. A second CriteriaQuery.from() call created an unnecessary cross join; reusing the existing product root removed it. section 7.3.3
  • Inspect connection status after reproducing the issue. Connections still open long after a request responds can indicate a leak; CPU profiling can provide a stack trace to the creating code. section 8.1
  • After changing the code, profile again to check the result. Correcting the connection leak stopped the connections from remaining open. section 8.1
Figure 7.14 The profiler can intercept any SQL query sent to the DBMS through the JDBC driver. Here, we spot an issue in the generated query—an unneeded cross join that causes performance problems.

The intercepted SQL makes the unnecessary cross join visible. section 7.3.3

A query that looks harmless on a small table

A criteria query for one product accidentally adds a second root. With only ten records, the cross join may look harmless; with larger tables, it can cause latency and duplicate rows. section 7.3.3

How Troubleshooting Java: Read, debug, and optimize JVM applications covers it

How deep it goes
Walks through intercepting SQL and checking connection lifetimes, then uses code changes and repeat profiling to verify fixes.
Examples
Uses a Hibernate criteria query that creates a cross join and a product endpoint whose database connections remain open.
What you can do after
Diagnose unexpected SQL and leaking connections, then use profiling to confirm the code changes resolve them.
How it connects
Links JDBC profiling to SQL queries, Hibernate criteria queries, JPA, and connection leaks.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java: Read, debug, and optimize JVM applications. The section links point to the parts of the book it draws on.