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 (Mapped Diagnostic Context)

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 db-util… Read More

The best way to log JDBC statements

Introduction 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.