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

标签 java mysql spring spring-boot tomcat8

我已经在 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/

相关文章:

model-view-controller - 使用Spring,映射到web.xml中的root,找不到静态资源

java - 在 Dropwizard yaml 中找不到 DatabaseConfiguration 子类中的属性(分层问题)

java - 从枚举类名获取对实现接口(interface)的 Java Enum 的引用

c# - Windows Azure 连接到 MySQL DB 中的 InvalidCastException

php - mysql/PHP mysql更新不更新数据库

mysql - 忽略 TDateTime 中的毫秒(相同 TDateTime 值相减不为 0)

java - Hibernate SessionFactory 如何使用 LocalSessionFactoryBean Autowiring ?

java - 将图像从 swift 上传到 java REST api

java - Spring Data JPA 和 Exists 查询

java - Spring Integration 5.0 + Project Reactor : controlling threads