Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

We were troubleshooting some troubling problems when we needed to see what was going on inside the MySql driver. This isn’t a standard thing, so what to do.

We were using SLF4J for our existing logging so we chose that path.

First we included the SLF4J Log4J bridge by adding the following to our pom.xml.

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>${slf4j.version}</version>
</dependency>

Then we had to modify the URL property for our datasource by adding

&logger=com.mysql.jdbc.log.Slf4JLogger&profileSQL=true

to the end of our URL property. There is one caveat. The ampersand needs to be escaped since it is part of XML. Also, we already had some MySql driver settings, so we needed the ampersand instead of the question mark.

<bean id="settingsDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"
      p:driverClassName="com.mysql.jdbc.Driver"
      p:url="jdbc:mysql://my.server.com:3306/mycontext?zeroDateTimeBehavior=convertToNull&amp;logger=com.mysql.jdbc.log.Slf4JLogger&amp;profileSQL=true"
      p:username="nunnya"
      p:password="bidness"
      p:defaultAutoCommit="false"
      p:maxWait="3000"
      p:testOnBorrow="true"
      p:testWhileIdle="true"
      p:minEvictableIdleTimeMillis="30000"
      p:timeBetweenEvictionRunsMillis="60000"
      p:removeAbandoned="true"
      p:suspectTimeout="3"
      p:removeAbandonedTimeout="5"
      p:logAbandoned="true"
      p:validationQuery="select now()"
      p:logValidationErrors="true"
      p:initialSize="2"
      p:maxActive="2"
      p:minIdle="2"
      p:maxIdle="2"/>

February 2nd, 2018

Posted In: Java, java ninja, Javaninja, log4j-over-slf4j, Logging, Logging configuration, MySql

Leave a Comment

LinkedIn Auto Publish Powered By : XYZScripts.com