Mysql Spring Boot JPA连接验证不工作

Mysql Spring Boot JPA连接验证不工作,mysql,spring,spring-data,spring-boot,Mysql,Spring,Spring Data,Spring Boot,我目前正在调试一个使用SpringBoot(1.1.2.Release)构建的小应用程序。如果连接丢失(由于生产中的等待超时或开发中的连接中断),我会遇到重新连接数据库的问题。我当前正在使用以下配置参数(application.properties): 这将产生以下数据源: org.apache.tomcat.jdbc.pool.DataSource@73e369e5{ConnectionPool[ defaultAutoCommit=null; defaultReadOnly=null; de

我目前正在调试一个使用SpringBoot(1.1.2.Release)构建的小应用程序。如果连接丢失(由于生产中的等待超时或开发中的连接中断),我会遇到重新连接数据库的问题。我当前正在使用以下配置参数(application.properties):

这将产生以下数据源:

org.apache.tomcat.jdbc.pool.DataSource@73e369e5{ConnectionPool[
defaultAutoCommit=null;
defaultReadOnly=null;
defaultTransactionIsolation=-1;
defaultCatalog=null;
driverClassName=com.mysql.jdbc.Driver;
maxActive=100;
maxIdle=100;
minIdle=10;
initialSize=2;
maxWait=30000;
testOnBorrow=true;
testOnReturn=false;
timeBetweenEvictionRunsMillis=5000;
numTestsPerEvictionRun=0;
minEvictableIdleTimeMillis=60000;
testWhileIdle=true;
testOnConnect=false;
password=********;
url=jdbc:mysql://localhost:3306/test?autoreconnect=true;
username=test;
validationQuery=SELECT 1;
;
validationQueryTimeout=-1;
validatorClassName=null;
validationInterval=30000;
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;
suspectTimeout=0;
alternateUsernameAllowed=false;
commitOnReturn=false;
rollbackOnReturn=false;
useDisposableConnectionFacade=true;
logValidationErrors=false;
propagateInterruptState=false;
ignoreExceptionOnPreLoad=false;
}
我现在的问题是,当一个连接丢失时,需要相当长的时间才能重新建立连接。同时,用户得到一个空页面,服务器端抛出异常。据我所知,testOnBorrow应该在每次使用之前测试连接,并且每30秒测试一次。但事实显然并非如此。当我查看mysql时,似乎每隔35秒就会发生一些事情,并且睡眠时间会重置,但我在应用程序日志中没有看到任何查询。验证查询似乎完全丢失了

我通过spring数据存储库访问数据库


我目前不知道该尝试什么。

尝试将连接池替换为tomcat,因为它似乎可以处理超时/连接丢失。如果HikariCP存在于类路径上而TomcatJDBC不存在,SpringBoot将自动配置HikariCP

根据这一点,这更像是一个一般性问题。我甚至怀疑它在没有spring boot的情况下是否能正常工作,因为数据源似乎配置正确。使用autoreconnect=true更像是在抓住最后一根稻草,希望它能有所帮助。我知道它会引发异常,但我的理解是,由于validationquery,连接池(tomcat jdbc)应该被捕获并重新连接到数据库,即使没有autoreconnect=true。您可以做什么来代替借阅时验证,您可能希望定期测试连接并退出已关闭/空闲的连接。看到了,谢谢,我想这帮了大忙。下次有机会我会试试的。我想你不能把它们放在application.properties中。您是否在某处配置了org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean bean?将其放入其引用的数据源bean中
org.apache.tomcat.jdbc.pool.DataSource@73e369e5{ConnectionPool[
defaultAutoCommit=null;
defaultReadOnly=null;
defaultTransactionIsolation=-1;
defaultCatalog=null;
driverClassName=com.mysql.jdbc.Driver;
maxActive=100;
maxIdle=100;
minIdle=10;
initialSize=2;
maxWait=30000;
testOnBorrow=true;
testOnReturn=false;
timeBetweenEvictionRunsMillis=5000;
numTestsPerEvictionRun=0;
minEvictableIdleTimeMillis=60000;
testWhileIdle=true;
testOnConnect=false;
password=********;
url=jdbc:mysql://localhost:3306/test?autoreconnect=true;
username=test;
validationQuery=SELECT 1;
;
validationQueryTimeout=-1;
validatorClassName=null;
validationInterval=30000;
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;
suspectTimeout=0;
alternateUsernameAllowed=false;
commitOnReturn=false;
rollbackOnReturn=false;
useDisposableConnectionFacade=true;
logValidationErrors=false;
propagateInterruptState=false;
ignoreExceptionOnPreLoad=false;
}