简体   繁体   English

Tomcat JDBC池中没有足够的空闲连接

[英]There are not enough idle connections in Tomcat JDBC pool

Given the following Tomcat JDBC connection settings: 给定以下Tomcat JDBC连接设置:

<Resource name="jdbc/pc4"
            maxActive="200"
            maxIdle="100"
            minIdle="50"
            initialSize="50"
            maxWait="15000"
            auth="Container"
            type="javax.sql.DataSource"
            username="....."
            password="....."
            testOnBorrow="true"
            testWhileIdle="true"
            validationQuery="select 1"
            driverClassName="com.mysql.jdbc.Driver"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            url="jdbc:mysql://server_address/db_name?autoReconnect=true&amp;autoReconnectForPools=true&amp;zeroDateTimeBehavior=convertToNull&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;socketTimeout=300000" />

And following MySQL parameters: 并遵循MySQL参数:

max_connections = 100000
wait_timeout = 31536000
interactive_timeout = 31536000

I would expect there to be at least 50 idle connections in connection pool at all time. 我希望连接池中始终至少有50个空闲连接。

But what really happens is: there are 50 connections when the server started, after a while, all the connections die except the last one. 但真正发生的是:服务器启动时有50个连接,一段时间后,除最后一个连接外,所有连接都死掉了。

Is there a mistake in my configuration? 我的配置有错误吗?

Environment: 环境:

  • Linux 3.4 64-bit Linux 3.4 64位
  • OpenJDK 7 OpenJDK 7
  • Tomcat 7 雄猫7
  • MySQL 5.5 MySQL 5.5

I am not an MySQL expert, but using Tomcat JDBC with Oracle for a while. 我不是MySQL专家,但在一段时间内使用Tomcat JDBC和Oracle。 Idle connections persist here as long as the app isn't shut down. 只要应用程序未关闭,空闲连接就会一直存在。 I would check connecion logs in MySQL and enable logging in Tomcat JDBC to see who is really shutting down connections. 我会检查MySQL中的连接日志并启用Tomcat JDBC中的日志记录,以查看谁真正关闭了连接。 Either one does. 任何一个都可以。

I don't know if this is the cause of your problem, but when I use the MySQL JDBC library, I always have a problem that if a connection is opened for a long time (more than 1 hour), it randomly causes a broken pipe when I use it again. 我不知道这是否是你问题的原因,但是当我使用MySQL JDBC库时,我总是遇到一个问题,即如果连接打开很长时间(超过1小时),它会随机导致断开当我再次使用它时管道。

Maybe the pool does not re-establish a connection after such an error (bug with the testWhileIdle?) Can you try to set removeAbandoned and removeAbandonedTimeout , with a value of, let's say, 30 minutes? 在这样的错误之后,池可能没有重新建立连接(testWhileIdle的错误?)你能尝试设置removeAbandonedremoveAbandonedTimeout ,值为30分钟吗? I know it's a long shot. 我知道这是一个很长的镜头。

This is an issue with mysql. 这是mysql的一个问题。 As @Djebel said try to clean the unused connection. 正如@Djebel所说,试着清理未使用的连接。 One question, does it start working if you restart mysql service whenever you get this error? 一个问题,如果你在收到此错误时重新启动mysql服务,它是否会开始工作? Also make sure the all the connections get closed while shutdown the server. 还要确保在关闭服务器时关闭所有连接。 You can use a shutdownHook, if Tomcat doesn't do it for you. 如果Tomcat没有为您执行此操作,则可以使用shutdownHook。

I changed the configuration and the situation seems to be improved, there are now always more than 25 connections in connection pool (still not sure why though). 我改变了配置,情况似乎有所改善,现在连接池中总有超过25个连接(但仍然不确定为什么)。

 <Resource name="jdbc/pc4"
            maxActive="-1"
            maxIdle="-1"
            minIdle="20"
            initialSize="20"
            maxWait="-1"
            auth="Container"
            type="javax.sql.DataSource"
            username="__"
            password="__"
            driverClassName="com.mysql.jdbc.Driver"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            testOnBorrow="true"
            testWhileIdle="true"
            testOnReturn="true"
            timeBetweenEvictionRunsMillis="60000"
            minEvictableIdleTimeMillis="60000"
            removeAbandoned="false"
            validationQuery="SELECT 1;"
            url="jdbc:mysql://__/__?autoReconnect=true&amp;autoReconnectForPools=true&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull&amp;socketTimeout=72000000" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM