0

I've reviewed a couple of questions/answers here around the subject, but nothing works out-of-the-box. I've also read the SqlLogger section in the official documentation, but still I can't find a way log/visualize what Jdbi (version 3.x) "is doing" when it interacts with the database? — in a straightforward way.

I'm aware Jdbi is using almost raw SQL, but it's always nice to be able to see what is it what the framework/library says it's doing for debugging purposes, etc.

I've tried pretty much any namespace starting from org.jdbi (within a logback.xml file), up until trace mode, but I just see something like this:

03-01-2021 19:52:26,656 |- TRACE in org.jdbi.v3.core.Jdbi:315 [reactor-http-epoll-2] - Jdbi [org.jdbi.v3.core.Jdbi@7a76fb45] obtain handle [org.jdbi.v3.core.Handle@725d5aec] in 0ms
03-01-2021 19:52:26,697 |- TRACE in org.jdbi.v3.core.Handle:187 [reactor-http-epoll-2] - Handle [org.jdbi.v3.core.Handle@725d5aec] released

Is there a way to do this these days?

x80486
  • 6,627
  • 5
  • 52
  • 111
  • I assume you have already looked at [`StatementContext`](https://jdbi.org/apidocs/org/jdbi/v3/core/statement/StatementContext.html) - accessible from your implementation of `SqlLogger`. If that is not suitable for your situation, maybe you could clarify where it falls short? Apart from the point that `ctx.getBinding().findForName()` is now deprecated ("_...keep your own state!_"), it has met my basic needs very well. – andrewJames Mar 02 '21 at 01:33
  • @andrewjames, what I'm looking for is a way to do what you can do with Hibernate's `show-sql`...thus, it's, a declarative way to do the same in Jdbi. I looked at that class, but I didn't get what you meant. – x80486 Mar 02 '21 at 01:45
  • 3
    OK, understood. My approach is not declarative. I create my own implementation of `SQlLogger`: `public class MyAppSqlLogger implements SqlLogger` - and then I override `logAfterExecution(StatementContext ctx)`. In that method I have access to all the logging data I need: SQL statements, SQL parameters, execution times. It's a small class. – andrewJames Mar 02 '21 at 02:08
  • 2
    `SqlLogger` is the supported approach for this, if I understand the question right. Disclaimer: I developed it. – Marnes Mar 02 '21 at 17:57

1 Answers1

1

Not a JDBI answer, but more generic way to see raw SQL is to use JDBC proxy like P6Spy or datasource-proxy.

P6Spy allows intercepting by either decorating DataSource or stub JDBC Driver (requires no code changes) and prints logs in format:

p6spy: #1617156635 | took 0ms | statement | connection 3|SELECT NOW()

Datasource-proxy only supports decorating DataSource and prints:

n.t.d.l.l.SLF4JQueryLoggingListener:
Name:, Time:0, Success:True
Type:Statement, Batch:False, QuerySize:1, BatchSize:0
Query:["SELECT NOW()"]
Params:[]
agavlyukovskiy
  • 231
  • 3
  • 11