java SpringBoot MySQL JDBC无法创建池的初始连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49024179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:14:36  来源:igfitidea点击:

SpringBoot MySQL JDBC Unable to create initial connections of pool

javamysqltomcatspring-boot

提问by Lezenford

Good day!

再会!

I have a simple springboot application with mysql jdbc repository.

我有一个带有 mysql jdbc 存储库的简单 springboot 应用程序。

I have properties for connect to DB

我有连接到数据库的属性

spring.datasource.url=jdbc:mysql://*:3306/*?useSSL=false
spring.datasource.username=*
spring.datasource.password=*
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.initialize=true
spring.datasource.dbcp2.validation-query=SELECT 1
spring.datasource.dbcp2.max-total=1

My test DB has only max 10 connections for user. When I use console

我的测试数据库只有最多 10 个用户连接。当我使用控制台时

SHOW STATUS WHERE variable_name = 'threads_connected';

I can see that now DB has 5 connections only but when I try to start my application I get Exception

我可以看到现在 DB 只有 5 个连接,但是当我尝试启动我的应用程序时出现异常

2018-02-28 10:26:24.115 ERROR 17360 --- [nio-8080-exec-3] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.

java.sql.SQLSyntaxErrorException: User '*' has exceeded the 'max_user_connections' resource (current value: 10)

2018-02-28 10:26:24.115 错误 17360 --- [nio-8080-exec-3] oatomcat.jdbc.pool.ConnectionPool:无法创建池的初始连接。

java.sql.SQLSyntaxErrorException:用户“*”已超出“max_user_connections”资源(当前值:10)

How can I fix it? And why I get that Exception if I have 5 free connetion on DB and I need only 1 connection for pool from properties? I can't edit max connection on DB because use Heroku like testDB. I can edit only tomcat properties only

我该如何解决?如果我在 DB 上有 5 个免费连接并且我只需要 1 个连接来自属性的池,为什么我会得到那个异常?我无法在 DB 上编辑最大连接数,因为像 testDB 一样使用 Heroku。我只能编辑 tomcat 属性

回答by g00glen00b

You configured the following property:

您配置了以下属性:

spring.datasource.dbcp2.max-total=1

This indicates that you're trying to use the DBCP 2connection pool. However, when you check the stacktrace, you can see the following:

这表明您正在尝试使用DBCP 2连接池。但是,当您检查堆栈跟踪时,您可以看到以下内容:

o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.

As the package of the ConnectionPoolclass is org.apache.tomcat, this indicates that you're actually using the default Tomcat connection pool. This means that your max-totalpoperty is not being picked up properly.

由于ConnectionPool该类的包是org.apache.tomcat,这表明您实际上正在使用默认的 Tomcat 连接池。这意味着您的max-totalpoperty 没有被正确拾取。

If you want to configure this for a Tomcat connection pool, you need to use the maxActiveproperty:

如果要为 Tomcat 连接池配置此项,则需要使用该maxActive属性:

spring.datasource.tomcat.max-active=1


Alternatively, if you don't want to use the Tomcat connection pool, you can add the DBCP 2 dependency using Maven/Gradle/... . If you exclude the default Tomcat connection pool, it will automatically pick up DBCP 2.

或者,如果您不想使用 Tomcat 连接池,您可以使用 Maven/Gradle/... 添加 DBCP 2 依赖项。如果排除默认的 Tomcat 连接池,它将自动选择 DBCP 2。

Another possibility is to configure it by using the spring.datasource.typeproperty as mentioned by the documentation:

另一种可能性是使用文档中spring.datasource.type提到的属性来配置它:

You can bypass that algorithm completely and specify the connection pool to use via the spring.datasource.typeproperty. This is especially important if you are running your application in a Tomcat container as tomcat-jdbc is provided by default.

您可以完全绕过该算法并通过spring.datasource.type属性指定要使用的连接池。如果您在 Tomcat 容器中运行应用程序,这一点尤其重要,因为默认情况下提供了 tomcat-jdbc。

For example:

例如:

spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource

回答by nightfury

I was stuck in similar situation but the simple fix for this could be like the database that you are pointing in application.propertiesprobably does not exist.

我陷入了类似的情况,但对此的简单修复可能就像您指向的数据库application.properties可能不存在一样。

Or You can try to delete org.springframework.data folder from C:\Users\{yourusername}\.m2folder and update Maven project.

或者您可以尝试从C:\Users\{yourusername}\.m2文件夹中删除 org.springframework.data 文件夹并更新 Maven 项目。

回答by SaravanaKumar KKB

I got exactly same error and It worked for me after changing the spring boot version from 1.5.* to 2.1.8.

我得到了完全相同的错误,在将 spring 引导版本从 1.5.* 更改为 2.1.8 后,它对我有用。

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>