[转]C3P0官方对于MySQL8小时问题的解决方案 The last packet successfully received from the server was x milliseconds

C3P0官方对于MySQL8小时问题的解决方案

之前在公司开发的时候,突然发现系统登录不上去了,一直报超时。以前还好好的,突然就不行了,可能是因为重启数据库导致的,但也不确定。检查了很多地方,只有重启tomcat才可以。然后就开始搜资料,发现原来是mysql的连接在8个小时候会关闭,系统无法自动连接,导致操作超时。记录下处理的过程。我们使用了c3p0组件和hibernate持久化数据层。

问题信息

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 56588 milliseconds ago.
The last packet sent successfully to the server was 56588 milliseconds ago, which 
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.

大致意思是说超时了,连接不可用,给的提示可以将autoReconnect=true来避免这一问题的发生

解决方案

  1. 将mysql服务器的wait_timeout设置为无穷大,保证永不超时(不建议采用)像这样
  2. 根据提示自动重连将autoReconnect=true添加到数据库链接的代码中,像这样:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false

不建议采用
3. 在mysql的wait_timeout时间之内发一次心跳,保证连接的有效性;
4. 在使用的时候检测连接的有效性,每隔一段时间就检测连接的有效性,失去就获取一个新链接

C3P0官方的建议

本文介绍一下C3P0官方推荐的方式,这种方式综合了第三和第四种解决方案

将testConnectionOnCheckout 设为 false
将testConnectionOnCheckin 设为 true
将idleConnectionTestPeriod 设为 30,这个数字要根据项目情况设定,比8小时小就好

解释一下这几个参数

testConnectionOnCheckout 设为 false
If true, an operation will be performed at every connection 
checkout to verify that the connection is valid.
设置为true,所有的连接都将检测其有效性,会影响性能,所以将其设置为false;

testConnectionOnCheckin设为 true
If true, an operation will be performed asynchronously 
at every connection checkin to verify that the connection is valid.
设置为true,异步检测连接的有效性

idleConnectionTestPeriod(单位是秒,不是毫秒)
If this is a number greater than 0, c3p0 will test all idle, 
pooled but unchecked-out connections, every this number of seconds.
每隔多少秒c3p0检测连接的有效性

示例

注意:
在c3p0配置的时候,要注意c3p0的版本以及配置文件的格式。

  <property name="hibernate.connection.provider_class">net.sf.hibernate.connection.C3P0ConnectionProvider</property>
  <property name="hibernate.c3p0.max_size">30</property><!--在连接池中所有数据库连接的最大数目-->
  <property name="hibernate.c3p0.min_size">5</property><!--在连接池中可用数据库连接的最小数目-->
  <property name="hibernate.c3p0.timeout">120</property><!--设定数据库连接的超时时间-->
  <property name="hibernate.c3p0.max_statements">100</property><!--可以被缓存的PreparedStatement的最大数目-->
  <property name="hibernate.c3p0.idle_test_period">3000</property>
  <property name="hibernate.c3p0.acquire_increment">2</property>
  <property name="hibernate.c3p0.validate">false</property>
  <!-- 连接池每隔60秒自动检测数据库连接情况,如果断开则自动重连 -->
  <property name="c3p0.idleConnectionTestPeriod">60</property>
  <property name="c3p0.testConnectionOnCheckout">true</property>

另一种格式

#c3p0反空闲设置,防止8小时失效问题28800
#---------------------------------------------------------
#idleConnectionTestPeriod要小于MySQL的wait_timeout
jdbc.c3p0.testConnectionOnCheckout=false
jdbc.c3p0.testConnectionOnCheckin=true
jdbc.c3p0.idleConnectionTestPeriod=3600

c3p0的简单配置就介绍这么多。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL报错"The last packet successfully received from the server was X milliseconds ago"时,这通常是由于连接超时引起的。解决这个问题的方法有以下几种: 1. 检查网络连接:首先确保网络连接正常,可以尝试使用其他网络连接或者重启网络设备。 2. 调整连接超时时间:可以通过修改MySQL配置文件中的"wait_timeout"参数来增加连接超时时间。该参数表示MySQL服务器在没有活动连接的情况下等待的时间,默认为8小时。可以将其增加到更大的值,例如设置为28800(8小时)。 3. 优化查询性能:如果查询语句执行时间过长,可能会导致连接超时。可以通过优化查询语句、创建索引、调整数据库结构等方式来提高查询性能,减少连接超时的可能性。 4. 增加服务器资源:如果服务器资源不足,可能会导致连接超时。可以考虑增加服务器的内存、CPU等资源,以提高服务器的处理能力。 5. 使用长连接:长连接是指在一个连接上可以执行多个查询,而不是每次查询都建立一个新的连接。使用长连接可以减少连接建立和断开的开销,降低连接超时的可能性。 6. 检查防火墙设置:有时防火墙设置可能会导致连接超时。确保防火墙没有阻止MySQL服务器的访问。 希望以上方法能够帮助您解决MySQL报错"The last packet successfully received from the server was X milliseconds ago"的问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值