JDBC Connection Interception

I have a superclass that has java.sql.Connection. There are many subclasses that use a join to execute select.

I need to intercept all the instructions executed through the connection and log the executable SQL.

How can i do this?

+6
source share
1 answer

Use log4jdbc . This is a JDBC proxy driver that logs all of your SQL and passes SQL and parameters to the base driver. Thus, your code does not even need to know about registration, all this is contained in a third-party library.

The configuration consists mainly of modifying your JDBC connection string to use a proxy instead of the real driver and specifying log4jdbc what the real JDBC driver is. Then you select the registration categories in your log4j configuration to indicate which things you want to register. You can get SQL, time information, connection open and close events, and the contents of a result set.

For a detailed explanation of how this works, see Jack Shirazi's book on tuning Java performance. There is an extended example of creating a proxy driver in the JDBC chapter.

+9
source

All Articles