Programming notes: Using Tomcat connection pool on Spring Framework.

By isendev.

 Posted on 2012/02/02 22:47.

 Tagged as: programming, java, springframework.

Recently, I switched from the outdated (but reliable) c3p0 connection pool I was using in my blogging software to the more recent Tomcat 7 integrated connection pool. This is a code snippet from my Spring Framework bean configuration file that shows you how to configure a Tomcat connection pool with a MySQL database:

<!--
  Deploy a in-memory MySQL configured datasource using the Tomcat's integrated
  connection pool (DBCP). IMPORTANT! It's necessary to copy the MySQL JDBC library
  (.jar) to Tomcat's library folder ($CATALINA_HOME/lib).
-->

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
 
  <!-- Database driver class name -->
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />      

  <!-- DEV database -->
  <property name="url" value="jdbc:mysql://127.0.0.1:3306/dblog" />

  <!-- PROD database -->
  <!-- <property name="url" value="jdbc:mysql://127.0.0.1:3306/pblog" /> -->

  <!-- Credentials -->
  <property name="username" value="myuser" />
  <property name="password" value="mypassword" />
      
  <!-- TOMCAT connection pool parameters -->      
  <property name="initialSize" value="0" />
  <property name="initSQL" value="SELECT * FROM USER" />      
  <property name="minIdle" value="10" />
  <property name="maxIdle" value="100" />
  <property name="maxActive" value="100" />
  <property name="maxWait" value="6000" />
  <property name="jmxEnabled" value="true" />
  <property name="jdbcInterceptors"
    value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
    org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer" />
  <property name="removeAbandoned" value="true" />
  <property name="removeAbandonedTimeout" value="60" />
  <property name="logAbandoned" value="true" />
  <property name="testOnBorrow" value="true" />
  <property name="testOnReturn" value="false" />
  <property name="testWhileIdle" value="false" />
  <property name="useEquals" value="false" />
  <property name="fairQueue" value="false" />
  <property name="timeBetweenEvictionRunsMillis" value="30000" />
  <property name="minEvictableIdleTimeMillis" value="30000" />
  <property name="validationInterval" value="1800000" />
  <property name="validationQuery" value="SELECT * FROM USER" />

</bean>


Tags
Archives