2

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 web.xml based application but not Spring Boot. I managed to register the servlet filter by adding this bean to an @Configuration class:

@Bean
public FilterRegistrationBean snifferFilter()
{
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    SnifferFilter filter = new SnifferFilter();
    filter.setInjectHtml(true);
    filterRegistrationBean.setFilter(filter);
    filterRegistrationBean.setName("sniffer");
    filterRegistrationBean.addUrlPatterns("/*");
    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 application.yml:

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

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

URL must start with 'jdbc'

Alexey Ogarkov
  • 2,916
  • 23
  • 28
Robert Hunt
  • 7,914
  • 5
  • 40
  • 43

2 Answers2

2

Sniffy author here!

Indeed as of version 3.0.7 (April 2016) you have to specify the driver class name explicitly in your Spring Boot application. There's an open issue in a bug tracker to configure it automatically.

By the way sniffy 3.0.5 introduced an out-of-the box support of Spring Boot using @EnableSniffy annotation, so you do not have to create the FilterRegistrationBean yourself anymore - just put the annotation to your application class like this:

import io.sniffy.boot.EnableSniffy;

@SpringBootApplication
@EnableAutoConfiguration
@EnableSniffy
public class Application {

    public static void main(String[] args) throws ClassNotFoundException {
        SpringApplication.run(Application.class, args);
    }

}
bedrin
  • 4,458
  • 32
  • 53
0

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
Robert Hunt
  • 7,914
  • 5
  • 40
  • 43
  • not working for sniffy 3.1.2, spring boot 1.5.2, oracle 11g, embedded tomcat. URL must start with 'jdbc' – tsogtgerel.ts Mar 23 '17 at 01:47
  • @ts.tsogoo Try the out-of-the-box support that was introduced in version 3.0.5 by adding `@EnableSniffy` to you Spring Boot application class. There so no need to modify the datasource url or driver-class-name anymore. – Robert Hunt Mar 29 '17 at 14:55
  • tnx Robert. but "EnableSniffy cannot be resolved to a type" error on Spring Boot application class – tsogtgerel.ts Mar 31 '17 at 04:15
  • @ts.tsogoo the `@EnableSniffy` annotation was added in Sniffy 3.1 so it should work as long as you're using the right version, see: http://sniffy.io/docs/latest/#_using_sniffy_with_spring – Robert Hunt Mar 31 '17 at 07:21