Spring boot无法验证新建立的连接

时间:2016-03-17 05:00:23

标签: java mysql spring spring-boot tomcat8

我已在Azure云中部署了Spring Boot Web应用程序。使用仅限4个连接的MySQL DB。以下是数据源属性

findById

由于我的应用程序丢失了数据库连接并且没有从中恢复,因此我添加了test-on-borrow属性。我正在使用JDBCTemplate。

我经常收到以下异常,但应用仍然有效。我需要知道这个异常是因为数据源属性还是有限的连接数(4)。如果我增加连接,这个例外就会消失。

异常消息

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

方法失败

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.

1 个答案:

答案 0 :(得分:4)

由于数据源属性,发生错误。通过添加test-on-borrow属性,您已指定应验证连接。验证失败,因为查询无效。删除查询中的引号以修复它:

spring.datasource.connection-test-query=SELECT 1