2014-06-25 83 views
6

我正在調試使用spring boot(1.1.2.Release)構建的小應用程序。如果連接丟失(由於生產中的wait_timeout或開發中的連接斷開),我遇到了重新連接到數據庫的問題。我目前使用以下配置參數(application.properties):Spring Boot JPA連接驗證不起作用

spring.datasource.url=jdbc:mysql://localhost:3306/test?autoreconnect=true 
spring.datasource.driverClassName=com.mysql.jdbc.Driver 
spring.datasource.test-on-borrow=true 
spring.datasource.test-while-idle=true 
spring.datasource.validation-query=SELECT 1; 
spring.datasource.initial-size=2 
... username+pw 

spring.jpa.generate-ddl=true 
spring.jpa.show-sql=true 

這將導致以下數據源:

[email protected]{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秒發生一次並且休眠時間會重置,但我在應用程序日誌中看不到任何查詢。驗證查詢似乎完全丟失。

我通過彈簧數據的存儲庫訪問數據庫。

我目前沒有進一步的想法什麼嘗試。

+0

根據這個http://stackoverflow.com/questions/667289/why-does-autoreconnect-true-not-seem-to-work它是更通用的問題。我甚至懷疑它沒有彈簧啓動工作正常,因爲數據源似乎配置正確。 –

+0

使用autoreconnect = true更多的是抓住最後一根稻草,希望它可以幫助。我知道它引發了一個異常,但我的理解是連接池(tomcat-jdbc)由於驗證查詢而應該被捕獲並重新連接到數據庫,即使沒有autoreconnect = true也是如此。 – ssindelar

+3

你可以做什麼,而不是通過借用驗證,你可能想要定期測試連接並逐出abrent/idle連接。請參閱http://www.tomcatexpert.com/blog/2010/04/01/configuring-jdbc-pool-high-concurrency –

回答

1

嘗試交換到HikariCP而不是tomcat爲您的連接池,因爲它似乎處理超時/連接丟失a lot better。 Spring Boot會自動配置HikariCP,如果它存在於類路徑中並且tomcat-jdbc不存在。