2
votes

I use spring jdbc template for app.. and deploy it in tomcat.. I want to use connection pool with tomcat jdbc. My connection configuration is

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3310/mydb" /> 
    <property name="username" value="***" /> 
    <property name="password" value="***" /> 
    <property name="maxWait" value="10000" />
    <property name="removeAbandonedTimeout" value="60" />
    <property name="removeAbandoned" value="true" />
    <property name="logAbandoned" value="false" />
    <property name="initialSize" value="10" /> 
    <property name="maxActive" value="100" /> 
    <property name="minIdle" value="10" /> 
</bean> 

I don't know how, but when I run some test, and check the max thread in mysql, it show that the active thread is more than the maxActive configured in the configuration. So, why the maxActive in the configuration not working? And how to make it work? For example, the maxActive is 100 but when I check in mysql, the active thread is more than the maxActive.

2

2 Answers

2
votes

maxActive (int) The maximum number of active connections that can be allocated from this pool at the same time. The default value is 100

maxIdle (int) The maximum number of connections that should be kept in the pool at all times. Default value is maxActive:100 Idle connections are checked periodically (if enabled) and connections that been idle for longer than minEvictableIdleTimeMillis will be released. (also see testWhileIdle)

So i should recommend you to use maxIdle too, example:

<property name="maxIdle" value="100">

But maybe there is a problem, if you could show the code of your connection management, it would be helpful.

Here is an interesting link of a connection problem with declarative and programmatic transaction management with Spring: Connection pool problem with Spring and programmatic transaction management

1
votes

I got the same problem with you and my tomcat version is tomcat 9.

Here is my solution: you should set maxTotal value 100 instead of maxActive.

maxTotal: Maximum number of database connections in pool. Make sure you configure your mysqld max_connections large enough to handle all of your db connections. Set to -1 for no limit.

FROM http://tomcat.apache.org/tomcat-9.0-doc/jndi-datasource-examples-howto.html.