Spring MDC Transaction Logging
Introduction In this article, we are going to see how to configure a Spring application to use MDC (Mapped Diagnostic Context) for transaction logging. This technique is going to help us inject the Persistence Context information as well as the associated database transaction identifier in all the log entries emitted from within a @Transactional service method.
The best way to log SQL statements with Spring Boot
Introduction In this article, I’m going to show you the best way to log SQL statements when using Spring Boot. Logging SQL queries is very important, as it allows you to validate the number of generated statements, the syntax of the auto-generated queries, as well as prove that JDBC batching works as expected.
How to get the SQL query from JPQL or JPA Criteria
Introduction In this article, I’m going to show you how you can get the auto-generated SQL query from a JPQL or JPA Criteria API entity query.
Hibernate slow query log
Introduction In this article, I’m going to show you how you can activate the slow query log when using JPA and Hibernate. This slow query log feature has been available since Hibernate ORM 5.4.5 and notifies you when the execution time of a given JPQL, Criteria API or native SQL query exceeds a certain threshold value you have previously configured.
How to intercept and modify SQL queries with the Hibernate StatementInspector
Introduction A very useful, yet lesser-known, Hibernate feature is the ability to intercept and modify any auto-generated SQL statement using the Hibernate StatementInspector utility. In this article, we are going to see how the Hibernate StatementInspector mechanism works.
How to log the database transaction id using MDC
Introduction In this article, I’m going to show you how you can log the database transaction id associated with a given SQL statement using the MDC (Mapped Diagnostic Context) feature offered by many logging frameworks. If you are writing data access code, you have to use logging. As I explained in this article, using a JDBC proxy tool like datasource-proxy or p6spy is the best approach to logging SQL statements. With datasource-proxy, you can easily build a JUnit extension to automatically detect N+1 query issues. For more details, check out the Hypersistence… Read More
The best way to log SQL statements with JDBC, JPA or Hibernate
Introduction In this article, I’m going to show you the best way to log SQL statements when using either JDBC, JPA, or Hibernate. Whenever you are using a data access framework that auto-generates statements on your behalf, it is mandatory to log all statements to ensure their effectiveness, as well as to assert the possible performance implications.