我已经在 Azure 云中部署了我的 Spring Boot Web 应用程序。使用仅限于 4 个连接的 MySQL 数据库。以下是数据源属性
spring.datasource.url=jdbc:mysql://hostname:3306/dbname
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.max-active=4
spring.datasource.connection-test-query="SELECT 1"
spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=true
由于我的应用程序失去了数据库连接并且无法从中恢复,所以我添加了 test-on-borrow 属性。我正在使用 JDBCTemplate。
我经常收到以下异常,但该应用程序仍然有效。我需要知道这个异常是因为数据源属性还是连接数有限(4)。如果我增加连接,这个异常就会消失。
异常消息
Could not get JDBC Connection; nested exception is java.sql.SQLException: Failed to validate a newly established connection.Failed to validate a newly established connection.
失败的方法
org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection
最佳答案
错误是由于数据源属性而发生的。通过添加 test-on-borrow 属性,您已指定应验证连接。验证失败,因为查询无效。删除查询中的引号以修复它:
spring.datasource.connection-test-query=SELECT 1
关于java - Spring boot 无法验证新建立的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36052007/