我下面有MySQL数据库的连接字符串。在一些闲置时间后,MySQL连接由于未重新连接而中断,因此在启动期间运行良好。
我们需要包括哪些其他属性以保持连接有效?
datasources:
default:
url: ${JDBC_URL:`jdbc:mysql://dbhost/DB?autoReconnect=true`}
driverClassName: com.mysql.cj.jdbc.Driver
username: ${JDBC_USER}
password: ${JDBC_PASSWORD}
dialect: MYSQL
错误
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: The last packet successfully received from the server was 41,608,139 milliseconds ago. The last packet sent successfully to the
server was 41,608,144 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
我在项目中包括了tomcat jar。
compileOnly "io.micronaut.data:micronaut-data-processor:$micronautDataVersion"
implementation "jakarta.persistence:jakarta.persistence-api:2.2.2"
implementation "io.micronaut.data:micronaut-data-jdbc:$micronautDataVersion"
implementation "io.micronaut.configuration:micronaut-jdbc-tomcat"
我添加了这些设置。它使连接保持活动状态。
datasources:
default:
pooled: true
jmxExport: true
url: ${JDBC_URL:`jdbc:mysql://dbhost/DB?autoReconnect=true`}
driverClassName: com.mysql.cj.jdbc.Driver
username: ${JDBC_USER}
password: ${JDBC_PASSWORD}
dialect: MYSQL
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: "SELECT 1"
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: "ConnectionState;StatementCache(max=200)"