spring.net 连接mysql_spring-boot web app在一段时间后失去连接MySQL ...

我有一个普通的spring boot 1.2.x web应用程序,带有一个嵌入式Tomcat 7.x容器并连接到一个RDS实例(运行MySQL 5.6).如果应用程序空闲一段时间(8小时?),然后它收到请求,则会抛出以下异常

** BEGIN NESTED EXCEPTION **

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

MESSAGE: The last packet successfully received from the server was39320 seconds ago.The last packet sent successfully to the server was 39320 seconds ago, whi

ch is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your ap

plication, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this proble

m.

STACKTRACE:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was39320 seconds ago.The last packet sent succe

ssfully to the server was 39320 seconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or t

esting connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection pr

operty 'autoReconnect=true' to avoid this problem.

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)

... trimmed more of the stacktrace ...

Caused by: java.net.SocketException: Broken pipe

at java.net.SocketOutputStream.socketWrite0(Native Method)

at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)

at java.net.SocketOutputStream.write(SocketOutputStream.java:159)

at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)

at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)

at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3227)

... 119 more

** END NESTED EXCEPTION **

在我的application.yml中(可以设置数据源,hibernate等的配置)我有以下内容(这是我在调用管理API / env时得到的一部分)

"applicationConfig: [classpath:/application.yml]#rds-profile":{

"spring.profiles":"rds-profile",

"spring.datasource.driverClassName":"com.mysql.jdbc.Driver",

"spring.datasource.url":"jdbc:mysql://rds-host:3306/mydb?user=mysqlusername&password=****",

"spring.datasource.schema":"classpath:/schema.sql",

"spring.datasource.username":"mysqlusername",

"spring.datasource.password":"******",

"spring.datasource.testOnBorrow":true,

"spring.datasource.validationQuery":"SELECT 1",

"spring.datasource.continueOnError":true,

"spring.datasource.timeBetweenEvictionRunsMillis":5000,

"spring.datasource.minEvictableIdleTimeMillis":5000,

"spring.datasource.max-active":500,

"spring.jpa.database-platform":"org.hibernate.dialect.MySQL5InnoDBDialect",

"spring.jpa.database":"MYSQL",

"spring.jpa.show-sql":false,

"spring.jpa.generate-ddl":false,

"spring.jpa.hibernate.ddl-auto":"none",

"spring.jpa.hibernate.dialect":"org.hibernate.dialect.MySQL5InnoDBDialect"

},

奇怪的是,当我调用管理API“/ configprops”时,我得到了这个(我不知道这是否是问题的根源?

"spring.datasource.CONFIGURATION_PROPERTIES":{

"prefix":"spring.datasource",

"properties":{

"platform":"all",

"data":null,

"driverClassName":"com.mysql.jdbc.Driver",

"password":"******",

"url":"jdbc:mysql://rds-host:3306/mydb?user=mysqlusername&password=****",

"schema":"classpath:/schema.sql",

"username":"mysqlusername",

"jndiName":null,

"xa":{

"dataSourceClassName":null,

"properties":{

}

},

"continueOnError":true,

"sqlScriptEncoding":null,

"separator":";",

"initialize":true

}

},

问题是:鉴于上述配置和细节,为什么我仍然得到“wait_timeout”异常?我希望在借用时测试连接,并且我希望JDBC连接池在没有可用的情况下创建有效连接…那么为什么我的应用程序在(8小时?)或不活动之后用完了有效连接?

谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Spring Boot解析MySQL binlog,你需要使用一个名为"debezium"的开源库,它提供了一个用于解析MySQL binlog的模块。 以下是使用Spring Boot和Debezium解析MySQL binlog的步骤: 1. 添加Debezium依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>io.debezium</groupId> <artifactId>debezium-embedded</artifactId> <version>1.6.0.Final</version> </dependency> ``` 2. 配置Debezium 添加以下配置类: ```java @Configuration public class DebeziumConfig { @Bean public Configuration debeziumConfiguration() { return Configuration.create() .with("connector.class", "io.debezium.connector.mysql.MySqlConnector") .with("offset.storage", "org.apache.kafka.connect.storage.FileOffsetBackingStore") .with("offset.storage.file.filename", "/path/to/offset/file/offset.dat") .with("offset.flush.interval.ms", 60000) .with("name", "my-sql-connector") .with("database.hostname", "localhost") .with("database.port", 3306) .with("database.user", "root") .with("database.password", "password") .with("database.server.id", 1) .with("database.server.name", "my-app-connector") .with("database.whitelist", "my-db") .build(); } @Bean public EmbeddedEngine debeziumEngine() { return EmbeddedEngine.create() .using(debeziumConfiguration()) .notifying(record -> { String recordValue = record.value() != null ? record.value().toString() : null; System.out.println(recordValue); }) .build(); } } ``` 这个配置类定义了Debezium连接MySQL所需的配置参数,并配置了一个EmbeddedEngine,它将接收来自Debezium的记录并将其打印到控制台。 3. 启动Debezium 启动应用程序并等待一段时间,以便Debezium连接MySQL并开始发送来自binlog的记录。当记录被发送时,它们将被打印到控制台。 以上就是使用Spring Boot和Debezium解析MySQL binlog的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值