【发布时间】:2017-08-18 04:43:08
【问题描述】:
我已经在 Apache Tomcat 8.5.16 中配置了 Apache DBCP2 JNDI 数据源。我在 context.xml 中的资源标签如下所示。
<Resource auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false"
username="root"
password="mypassword"
name="jdbc/myDS"
initialSize="5"
minIdle="5"
maxIdle="10"
maxTotal="20"
testOnCreate="true"
testOnBorrow="true"
testOnReturn="true"
testWhileIdle="true"
validationQuery="SELECT 1 from dual"
validationQueryTimeout="60"
timeBetweenEvictionRunsMillis="180000"
numTestsPerEvictionRun="10"
softMinEvictableIdleTimeMillis="150000"
maxConnLifetimeMillis="300000"
logAbandoned="true"
removeAbandonedOnBorrow="true"
removeAbandonedOnMaintenance="true"
removeAbandonedTimeout="60"
maxWaitMillis="60000" />
我的问题是:
- 我启动了 tomcat(我还没有在 tomcat 上部署任何应用程序)。我打开 MYSQL 客户端,以 root 身份登录。现在,当我运行命令 show processlist 时,我看到了 31 个连接。 1个来自MYSQL客户端,30个来自tomcat。为什么当我的 maxTotal 为 20 时 DBCP2 获得了 30 个连接?不,还有一个是使用 MYSQL,而不是 tomcat 和 MYSQL 客户端。
- 由于我没有在 tomcat 上部署任何应用程序来使用 JNDI 数据源,因此我从 DBCP2 的所有连接都是空闲的。 timeBetweenEvictionRunsMillis="180000" 不会删除这些空闲连接。
- 当我删除 minIdle="5"。然后在 180000 毫秒后,所有 30 个 MYSQL 连接都从连接池中删除,连接池大小变为零。
帮助我理解这种行为。谢谢是提前。
【问题讨论】: