微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

spring-boot web app在一段时间后失去连接MySQL / RDS的能力

我有一个普通的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小时?)或不活动之后用完了有效连接?

谢谢.

解决方法:

尝试使用它

spring.datasource.test-while-idle=true
spring.datasource.validation-interval=5000

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐