Robert Hunt Robert Hunt - 5 months ago 21x
Java Question

How do I configure Sniffy with Spring Boot?

Sniffy is a cool little project:

Sniffy counts the number of executed SQL queries and provides an API for validating them It is designed for unit tests and allows you to test if particular method doesn't make more than N SQL queries Especially it's useful to catch the ORM N+1 problem at early stages

It also provides a servlet filter which injects HTML into a page with a popup showing you executed queries. The documentation explains how to configure it for a traditional
based application but not Spring Boot. I managed to register the servlet filter by adding this bean to an

public FilterRegistrationBean snifferFilter()
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
SnifferFilter filter = new SnifferFilter();
return filterRegistrationBean;

I also updated the JDBC url, the documentation says:

add sniffer: prefix to the JDBC connection url For example jdbc:h2:~/test should be changed to sniffer:jdbc:h2:mem:

So I added the following to my

spring.datasource.url: sniffer:jdbc:mysql://localhost:3306

But when I start my application it fails with this error:

URL must start with 'jdbc'


I managed to figure out the problem, Spring Boot makes extensive use of auto configuration and was trying to detect the DatabaseDriver from the connection string. As the connection string no longer starts with jdbc it was encountering a problem.

It was simply a case of specifying the driver-class-name in my application.yml rather than letting Spring Boot trying to auto detect it:

spring.datasource.driver-class-name: io.sniffy.MockDriver