使用Spring Boot Live Reload时如何停止MySQL用尽连接? CannotGetJdbcConnectionException:“连接太多”

时间:2018-12-03 21:21:57

标签: java mysql spring spring-boot

在更改了一些代码之后,Spring Boot进入了一个时髦的状态,并用堆栈跟踪信息淹没了我的控制台日志,并说它无法获得数据库连接。 Spring Boot如何通过Live Reload避免此问题?如果我杀死它并用mvn spring-boot:run重新运行它,它就可以工作(但是要花更长的时间)。

  

由以下原因引起:org.springframework.jdbc.CannotGetJdbcConnectionException:无法获取JDBC连接。嵌套的异常是com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:数据源拒绝建立连接,来自服务器的消息:“连接太多”

我在pom.xml中安装了Spring DevTools以进行实时重载。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <optional>true</optional>
</dependency>

这是我启动时的DataSource。默认testOnBorrow为true。

  

2018-12-03 16:09:39.530信息14804--[restartMain] c.s.s.TheApp:DATASOURCE = org.apache.tomcat.jdbc.pool.DataSource@72d10b82 {ConnectionPool [defaultAutoCommit = null; defaultReadOnly = null; defaultTransactionIsolation = -1; defaultCatalog = null; driverClassName = com.mysql.jdbc.Driver; maxActive = 100; maxIdle = 100; minIdle = 10; initialSize = 10; maxWait = 30000; testOnBorrow = true; testOnReturn = false; timeBetweenEvictionRunsMillis = 5000; numTestsPerEvictionRun = 0; minEvictableIdleTimeMillis = 60000; testWhileIdle = false; testOnConnect = false;密码= ********; url = jdbc:mysql:// localhost:3306 / appdb?useSSL = false;用户名=根; validateQuery = / * ping * / SELECT 1; validationQueryTimeout = -1; validatorClassName = null; validationInterval = 3000; accessToUnderlyingConnectionAllowed = true; removeAbandoned = false; removeAbandonedTimeout = 60; logAbandoned = false; connectionProperties = null; initSQL = null; jdbcInterceptors = null; jmxEnabled = true; fairQueue = true; useEquals = true; abandonWhenPercentageFull = 0; maxAge = 0; useLock = false; dataSource = null; dataSourceJNDI = null; believeTimeout = 0; AlternativeUsernameAllowed = false; commitOnReturn = false; rollbackOnReturn = false; useDisposableConnectionFacade = true; logValidationErrors = false; broadcastInterruptState = false; ignoreExceptionOnPreLoad = false; useStatementFacade = true; }

1 个答案:

答案 0 :(得分:0)

根据plt.hist(a, density = True ,cumulative=True) plt.show() ,您的池的连接大小为10到100。 100是很多连接。如果您打开了100个连接,则表示您是:

  1. 从池中获取后不关闭org.apache.tomcat.jdbc.pool.DataSource对象。必须关闭Connection对象,以便池可以重用它。确保Connection被呼叫的一种方法是始终使用close()

    try-with-resource
  2. 在短时间内打开所有它们,就没有机会触发30秒超时(例如,您设置的try (Connection conn = dataSource.getConnection()) { ... } 选项)。 maxWait=30000个对象可以重用,请参见HikariCP wiki: About Pool Sizing

不幸的是,告诉发生了什么的唯一方法是调试应用程序,并查看为什么连接用尽了。

相关问题