我使用 SpringBoot 2.2.10,Hikari 管理連接池并使用 JPA 從資料庫 Mysql 中查詢以創建微服務。有一個測驗場景,我的微服務應該在資料庫重啟/重啟后運行,但沒有成功。我確實嘗試在互聯網和 SO 上找到解決方案,并且有很多類似的問題,有些問題甚至在幾年前就被問到了,但我完全是個菜鳥,因為在我的情況下沒有一個人可以作業。下面是我的配置和堆疊跟蹤
spring.datasource.jdbcUrl=jdbc:mysql://xxx.xxx.xxx.xxx:3306/core
spring.datasource.username=admin
spring.datasource.password=1
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.maximumPoolSize=10
spring.datasource.idleTimeout=30000
spring.datasource.maxLifeTime=1800000
spring.datasource.connectionTestQuery=SELECT 1 FROM DUAL
和我的資料源配置類:
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class HikariCoreConfig extends HikariConfig {
@Bean
@public DataSource dataSource() {
return new HikariDataSource(this);
}
}
在啟動時,我的應用程式作業正常,可以從 db 正常讀取和寫入。在我殺死然后再次啟動資料庫行程之后,每當我寫入資料庫(使用repository.save(object))時,我都會不斷收到例外:
CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is ...JDBCConnectionException: Unable to acquire JDBC Connection
(我保證 mysql 正在運行,我可以從本地機器通過 Workbench 連接和查詢)
這是完整的堆疊跟蹤



(對不起,我無法提供文字)
我在互聯網上閱讀了許多網站,人們說 Hikari 會自動處理重新連接的事情。我做錯了什么還是我錯過了什么?請幫忙,謝謝!
uj5u.com熱心網友回復:
我發現了問題。Hikari 確實重新連接,我的所有配置都設定正確。我的情況發生在我使用以下方法在系統屬性中設定密鑰庫/信任庫時:
System.setProperty("javax.net.ssl.keyStore", keyStorePath);
System.setProperty("javax.net.ssl.keyStorePassword", kspassword);
System.setProperty("javax.net.ssl.trustStore", trustStorePath);
System.setProperty("javax.net.ssl.trustStorePassword", tspassword);
它使資料庫重新啟動后連接不可用。解決方案就是在 jdbcurl 中放兩個引數:&useSSL=false&allowPublicKeyRetrieval=true:
spring.datasource.jdbcUrl=jdbc:mysql://xxx.xxx.xxx.xxx:3306/core?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
那會做的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/408035.html
標籤:
上一篇:如何修復無法延遲初始化集合
