我不知道为什么每次有人访问我网站上的页面时,都会向数据库打开一个新连接。最终我达到300并得到一个错误,页面不再加载。我认为它应该工作的方式是,我将maxIdle设置为30,这意味着我不应该有超过30个睡眠连接由Spring打开。但此刻我有88开放。如果我关闭或重新启动我的服务器,它将返回到接近0.我对每个查询都使用getJdbcTemplate()。
这是我的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>
这是dao看起来像(重命名)的部分
@Repository
public class ExampleDAOImpl extends JdbcDaoSupport implements ExampleDAO {
@Autowired
public ExampleDAOImpl(DataSource dataSource) {
setDataSource(dataSource);
}
以下是我从MySQL中的show processlist获得的内容(此处未显示所有行,共有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)
示例dao方法
public String selectSomething(String text) throws IllegalArgumentException, DAOException {
return (String) getJdbcTemplate().queryForObject(SELECT_QUERY_1, new Object[] {text}, String.class);
}
答案 0 :(得分:1)
尝试此配置,这是对PROD环境进行许多调整的结果:
<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>