I don't know why each time someone visits a page on my website, a new connection is open to the database. Eventually I reach like 300 and get an error and pages no longer load. I thought the way it should work is, I Have maxIdle set for 30, so that means I should not have more than 30 sleeping connections open by Spring. But at this moment I have 88 open. If I close down or reboot my server it will go back down to near 0. I use getJdbcTemplate() for every query.
Here is part of my spring-config.xml
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="XXXXXXXXX" />
<property name="username" value="XXXXXXXXXX" />
<property name="password" value="XXXXXXXX" />
<property name="maxIdle" value="30"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
Here is what part the dao looks like (renamed)
@Repository
public class ExampleDAOImpl extends JdbcDaoSupport implements ExampleDAO {
@Autowired
public ExampleDAOImpl(DataSource dataSource) {
setDataSource(dataSource);
}
Here is what I get from show processlist in MySQL (not display all rows here, there were 88)
+----------+------------+-------------------------------------------+------+---------+-------+----------------+---------------------
| Id | User | Host | db | Command | Time | State | Info db
+----------+------------+-------------------------------------------+------+---------+-------+----------------+-----------------------
| 21721763 | user | XXXXXXXX:60586 | db | Sleep | 10609 | | NULL db
| 21924432 | user | XXXXXXXX:59498 | db | Query | 0 | NULL | show processlist db
| 21924580 | user | XXXXXXXX:59702 | db | Sleep | 790 | | NULL db
| 21924722 | user | XXXXXXXX:60010 | db | Sleep | 790 | | NULL db
| 21924813 | user | XXXXXXXX:60131 | db | Sleep | 787 | | NULL db
| 21924819 | user | XXXXXXXX:60139 | db | Sleep | 786 | | NULL db
| 21924841 | user | XXXXXXXX:60156 | db | Sleep | 785 | | NULL db
| 21927089 | user | XXXXXXXX:56292 | db | Query | 1 | Opening tables | XXXXXXXX |
| 21927090 | user | XXXXXXXX:56295 | db | Query | 1 | Opening tables | XXXXXXXX |
88 rows in set (0.01 sec)
Example dao method
public String selectSomething(String text) throws IllegalArgumentException, DAOException {
return (String) getJdbcTemplate().queryForObject(SELECT_QUERY_1, new Object[] {text}, String.class);
}
Try this configuration, which is the result of many tweaks for a PROD environment:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
<property name="url" value="XXX" />
<property name="username" value="XXX"/>
<property name="password" value="XXX"/>
<property name="minIdle" value="0"/>
<property name="maxIdle" value="10"/>
<property name="maxActive" value="50"/>
<property name="maxWait" value="60000"/>
<property name="testOnBorrow" value="true"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="50"/>
<property name="minEvictableIdleTimeMillis" value="10"/>
<property name="testWhileIdle" value="true"/>
</bean>