a notebook and some technical devices

Enable DB2 JDBC-Driver logging

While doing a little research for my blogpost “Timeout Configuration in HikariCP, DB2 and MySQL” I stumbled upon a nice feature of the DB2 JDBC-Driver called logWriter.

According to the DB2 for z/OS 10.0.0 Reference you can set an (optional) character output stream to which all logging and trace messages are printed. This includes all SQL-statements as well as all values needed to insert into a prepared statement. If no output stream is set (= null) this feature is simply disabled.

Let’s say you are using Spring then you can turn this feature on using Java Config:

DB2SimpleDataSource db2DataSource = new DB2SimpleDataSource();
db2DataSource.setLogWriter(new PrintWriter(System.out));
// some more configuration

Or using Xml Config:

<bean id="dataSourceDB2" class="com.ibm.db2.jcc.DB2SimpleDataSource">
	<property name="logWriter">
		<bean class="java.io.PrintWriter">
				<util:constant static-field="java.lang.System.out" />
	<!-- some more configuration -->

(Please note that printing to System.out is a reason to slap yourself. Don’t do this in production.)

Afterwards the log looks like:

14:10:37,792 INFO  [stdout] (http-localhost/ [jcc][Time:2015-08-27-14:10:37.792][Thread:http-localhost/][Connection@6eb1f4fb] prepareStatement (select sometable0_.TRMNUMMER as TRMNUMME1_3_0_ from SOME_TABLE sometable0_ left outer join ANOTHER_TABLE anothertable1_ on sometable0_.TRMNUMMER=anothertable1_.TRMNUMMER where sometable0_.TRMNUMMER=?) called
14:10:37,793 INFO  [stdout] (http-localhost/ [jcc][Time:2015-08-27-14:10:37.793][Thread:http-localhost/][Connection@6eb1f4fb] prepareStatement () returned com.ibm.db2.jcc.am.pn@52814790
14:10:37,793 INFO  [stdout] (http-localhost/ [jcc][SystemMonitor:stop] core: 0.560809ms | network: 0.0ms | server: 0.0ms
14:10:37,793 INFO  [stdout] (http-localhost/ [jcc][Time:2015-08-27-14:10:37.793][Thread:http-localhost/][PreparedStatement@52814790] setLong (1, 100238731) called
14:10:37,793 INFO  [stdout] (http-localhost/ [jcc][SystemMonitor:start] 
14:10:37,794 INFO  [stdout] (http-localhost/ [jcc][Time:2015-08-27-14:10:37.794][Thread:http-localhost/][PreparedStatement@52814790] executeQuery () called

Rolf Engelhard


Leave a Reply

Required fields are marked *.