Spring connection pool configuration

I've searched the internet for a while and I haven't solved this problem yet:

I have the following data source configuration:

 <bean id="cpms.prod.ds"  class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
    <property name="url"><value>jdbc:mysql://localhost/mysql</value></property>
    <property name="username"><value>test</value></property>
    <property name="password"><value>test</value></property>
    <property name="initialSize" value="1" />
    <property name="maxActive" value="2" />
    <property name="maxIdle" value="1"></property>
 </bean>

      

This should be enough to make sure that there are only 2 active connections at one point, and the ones used for the pool.

In my java code I am using SimpleJdbcTemplate

to make three requests and as far as I understand this object should return connections to the pool after each request completes, should also block the third request while one of the others end.

When I view my database admin console, 3 connections appear and then go into a pending state. If I run the queries again, I see 3 more popups and the other 3 stay there.

The only way I have found for closed connections is to set:

<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="1"/>

<property name="minIdle" value="0"></property>
<property name="timeBetweenEvictionRunsMillis" value="1000"></property>
<property name="minEvictableIdleTimeMillis" value="1000"></property>

      

which makes the abandoned connections procedure start and clean up old connections.

I don't need to tamper with these parameters, and especially not set them as low as they might have performance issues.

I also tried the solution shown here , until I changed the values timeBetweenEvictionRunsMillis

and minEvictableIdleTimeMillis

to lower values, And it still does not restrict connections to 2.

+3


source to share


1 answer


All connections in the JdbcTemplate are through DataSourceUtils.doGetConnection . What you see may be related to "intelligence" in BasicDataSource

From the API:

Abandonded connections are identified and removed when getConnection() is invoked and the following conditions hold

:



  • getRemoveAbandoned() = true

  • getNumActive ()> getMaxActive () - 3
  • getNumIdle() < 2

The data source appears to allow 3 more active connections than the max-active specified.

0


source







All Articles