How to debug SQL statements in Spring Boot applications

In order to debug your SQL Statements in Spring Boot applications, you can use the following settings in your application.properties file:

logging.level.org.hibernate.SQL=DEBUG 
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

When this configuration is added, you will be able to see:

1) Each SQL Statement executed

2) The timing information so that you can find slow SQL queries

3) The single parameters used to bind your Prepared Statements

Here is a sample log, when you have activated the above settings:

2021-03-19 11:00:55.979 DEBUG 8696 --- [           main] org.hibernate.SQL                        : drop table person if exists 2021-03-19 11:00:55.980 DEBUG 8696 --- [           main] org.hibernate.SQL                        : drop sequence if exists hibernate_sequence 2021-03-19 11:00:55.984 DEBUG 8696 --- [           main] org.hibernate.SQL                        : create sequence hibernate_sequence start with 1 increment by 1 2021-03-19 11:00:55.986 DEBUG 8696 --- [           main] org.hibernate.SQL                        : create table person (id bigint not null, name varchar(255), surname varchar(255), primary key (id)) 2021-03-19 11:00:55.995  INFO 8696 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] 2021-03-19 11:00:56.001  INFO 8696 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2021-03-19 11:00:56.539  WARN 8696 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2021-03-19 11:00:56.759  INFO 8696 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor' 2021-03-19 11:00:56.939  WARN 8696 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration) 2021-03-19 11:00:57.077  INFO 8696 --- [           main] c.e.samplewebapp.DemoApplicationTests    : Started DemoApplicationTests in 4.732 seconds (JVM running for 5.737) 2021-03-19 11:00:57.094 DEBUG 8696 --- [           main] org.hibernate.SQL                        : call next value for hibernate_sequence 2021-03-19 11:00:57.126 DEBUG 8696 --- [           main] org.hibernate.SQL                        : insert into person (name, surname, id) values (?, ?, ?) 2021-03-19 11:00:57.130 TRACE 8696 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [Jack] 2021-03-19 11:00:57.131 TRACE 8696 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [Smith] 2021-03-19 11:00:57.131 TRACE 8696 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [BIGINT] - [1] 2021-03-19 11:00:57.135 DEBUG 8696 --- [           main] org.hibernate.SQL                        : call next value for hibernate_sequence 2021-03-19 11:00:57.137 DEBUG 8696 --- [           main] org.hibernate.SQL                        : insert into person (name, surname, id) values (?, ?, ?) 2021-03-19 11:00:57.137 TRACE 8696 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [Joe] 2021-03-19 11:00:57.137 TRACE 8696 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [Black] 2021-03-19 11:00:57.137 TRACE 8696 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [BIGINT] - [2] 2021-03-19 11:00:57.139 DEBUG 8696 --- [           main] org.hibernate.SQL                        : call next value for hibernate_sequence 2021-03-19 11:00:57.139 DEBUG 8696 --- [           main] org.hibernate.SQL                        : insert into person (name, surname, id) values (?, ?, ?) 2021-03-19 11:00:57.140 TRACE 8696 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [Martin] 2021-03-19 11:00:57.140 TRACE 8696 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [Bauer] 2021-03-19 11:00:57.140 TRACE 8696 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [BIGINT] - [3] 2021-03-19 11:00:57.141  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   : Persons found with findAll(): 2021-03-19 11:00:57.141  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   : ------------------------------- 2021-03-19 11:00:57.260 DEBUG 8696 --- [           main] org.hibernate.SQL                        : select person0_.id as id1_0_, person0_.name as name2_0_, person0_.surname as surname3_0_ from person person0_ 2021-03-19 11:00:57.268  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   : Person [id=1, name=Jack, surname=Smith] 2021-03-19 11:00:57.268  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   : Person [id=2, name=Joe, surname=Black] 2021-03-19 11:00:57.268  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   : Person [id=3, name=Martin, surname=Bauer] 2021-03-19 11:00:57.268  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   :  2021-03-19 11:00:57.273 DEBUG 8696 --- [           main] org.hibernate.SQL                        : select person0_.id as id1_0_0_, person0_.name as name2_0_0_, person0_.surname as surname3_0_0_ from person person0_ where person0_.id=? 2021-03-19 11:00:57.274 TRACE 8696 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [BIGINT] - [1] 2021-03-19 11:00:57.279  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   : Person found with findById(1L): 2021-03-19 11:00:57.279  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   : -------------------------------- 2021-03-19 11:00:57.279  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   : Person [id=1, name=Jack, surname=Smith] 2021-03-19 11:00:57.279  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   :  2021-03-19 11:00:57.279  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   : Person found with findBySurname('Black'): 2021-03-19 11:00:57.279  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   : -------------------------------------------- 2021-03-19 11:00:57.310 DEBUG 8696 --- [           main] org.hibernate.SQL                        : select person0_.id as id1_0_, person0_.name as name2_0_, person0_.surname as surname3_0_ from person person0_ where person0_.surname=? 2021-03-19 11:00:57.311 TRACE 8696 --- [           main] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [Black] 2021-03-19 11:00:57.313  INFO 8696 --- [           main] c.example.samplewebapp.DemoApplication   : Person [id=2, name=Joe, surname=Black]