Is it possible to get the SQL query causing a Spring DataAccessException?

Solution 1:

If you only intend to log SQL statements during an exception, you might have to write your own custom subclass of JdbcTemplate and alter the logging preconditions as seen in the source code at Github.

If that is not the case, you may consider the following.

From the Spring documentation, All SQL Statements are logged at DEBUG level.

All SQL issued by this class is logged at the DEBUG level under the category corresponding to the fully qualified class name of the template instance (typically JdbcTemplate, but it may be different if you are using a custom subclass of the JdbcTemplate class).

You make also change the Jdbc url by setting profileSQL to true to trace the SQL.

MySQl Connection Reference Documentation