Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

We are using -D system parameters to define our connection pool values. They were defined in a Tomcat setenv file in the bin directory.

export CATALINA_OPTS="$CATALINA_OPTS -DPROXY-RO-JDBC=jdbc:mysql://192.168.0.1/javaninja?zeroDateTimeBehavior=convertToNull"
export CATALINA_OPTS="$CATALINA_OPTS -DPROXY-RO--- USER=javaninja"
export CATALINA_OPTS="$CATALINA_OPTS -DPROXY-RO-PASS=javaninja"
export CATALINA_OPTS="$CATALINA_OPTS -DPROXY-RO-INITIAL-SIZE=1"
export CATALINA_OPTS="$CATALINA_OPTS -DPROXY-RO-MIN-IDLE=1"
export CATALINA_OPTS="$CATALINA_OPTS -DPROXY-RO-MAX-IDLE=1"
export CATALINA_OPTS="$CATALINA_OPTS -DPROXY-RO-MAX-TOTAL=15"

The parameters appeared to be read correctly, as evidenced by the Tomcat log.

14-Dec-2016 09:23:41.499 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DPROXY-RO-JDBC=jdbc:mysql://192.168.0.1/javaninja?zeroDateTimeBehavior=convertToNull
14-Dec-2016 09:23:41.499 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DPROXY-RO--- USER=javaninja
14-Dec-2016 09:23:41.499 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DPROXY-RO-PASS=javaninja
14-Dec-2016 09:23:41.499 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DPROXY-RO-INITIAL-SIZE=1
14-Dec-2016 09:23:41.499 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DPROXY-RO-MIN-IDLE=1
14-Dec-2016 09:23:41.499 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DPROXY-RO-MAX-IDLE=1
14-Dec-2016 09:23:41.499 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DPROXY-RO-MAX-TOTAL=15

This did not work. We would get weird messages about the value not being in range. This was due to one of the values having noAccessToProcedureBodies=true. The JDBC driver expected yes, no, true or false Unfortunately, what it was receiving was true\n. I have no idea where the newline was coming from. I deleted the end of the line and typed the characters myself, but they were still there.

The solution was to trim all of the values of control characters. Apache StringUtils is my preferred tool for that; however, it is a static class and Spring doesn’t like that. To get over that, I instantiated a copy of it.

<bean id="stringUtils" class="org.apache.commons.lang3.StringUtils"/>

Once I had a StringUtils bean, I used it to trim the values by

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"
      p:driverClassName="com.mysql.jdbc.Driver"
      p:url="#{stringUtils.trim(systemProperties['PROXY-RO-JDBC'])}"
      p:username="#{stringUtils.trim(systemProperties['PROXY-RO--- USER'])}"
      p:password="#{stringUtils.trim(systemProperties['PROXY-RO-PASS'])}"
      p:initialSize="#{stringUtils.trim(systemProperties['PROXY-RO-INITIAL-SIZE'])}"
      p:maxTotal="#{stringUtils.trim(systemProperties['PROXY-RO-MAX-TOTAL'])}"
      p:maxIdle="#{stringUtils.trim(systemProperties['PROXY-RO-MAX-IDLE'])}"
      p:minIdle="#{stringUtils.trim(systemProperties['PROXY-RO-MIN-IDLE'])}"
      p:testOnBorrow="false"
      p:timeBetweenEvictionRunsMillis="60000"
      p:testWhileIdle="true"/>

December 14th, 2016

Posted In: Java, java ninja, Javaninja, Spring

Leave a Comment

LinkedIn Auto Publish Powered By : XYZScripts.com