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&logger=com.mysql.jdbc.log.Slf4JLogger&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"/>
sheltonn February 2nd, 2018
Posted In: Java, java ninja, Javaninja, log4j-over-slf4j, Logging, Logging configuration, MySql