在连接字符串中  添加设置节点 ConnectionLifeTime(计量单位为 秒)。超过设定的连接会话 会被杀死!

Connection Lifetime, ConnectionLifeTime

0

When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime. This is useful in clustered configurations to force load balancing between a running server and a server just brought online. A value of zero (0) causes pooled connections to have the maximum connection timeout.

    public bool ConnectionLifetimeExpired()
{
TimeSpan ts = DateTime.Now.Subtract(creationTime);
if (Settings.ConnectionLifeTime != &&
ts.TotalSeconds > Settings.ConnectionLifeTime)
return true;
return false;
}

MySQL 的默认设置下,当一个连接的空闲时间超过8小时后,MySQL 就会断开该连接,而 c3p0 连接池则以为该被断开的连接依然有效。在这种情况下,如果客户端代码向 c3p0 连接池请求连接的话,连接池就会把已经失效的连接返回给客户端,客户端在使用该失效连接的时候即抛出异常

解决这个问题的办法有三种:

1. 增加 MySQL 的 wait_timeout 属性的值。

修改 /etc/mysql/my.cnf文件,在 [mysqld] 节中设置:

# Set a connection to wait 8hours in idle status.
wait_timeout =86400
相关参数,红色部分
mysql> show variables like '%timeout%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| connect_timeout | 5 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| interactive_timeout | 28800 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| wait_timeout | 28800 |
+--------------------------+-------+
同一时间,这两个参数只有一个起作用。到底是哪个参数起作用,和用户连接时指定的连接参数相关,缺省情况下是使用wait_timeout。我建议是将这两个参数都修改,以免引起不必要的麻烦。

这两个参数的默认值是8小时(60*60*8=28800)。我测试过将这两个参数改为0,结果出人意料,系统自动将这个值设置为。换句话说,不能将该值设置为永久。
将这2个参数设置为24小时(60*60*24=604800)即可。
set interactive_timeout=604800;
set wait_timeout=604800;

2. 减少连接池内连接的生存周期,使之小于上一项中所设置的 wait_timeout 的值。
修改 c3p0 的配置文件,设置:

# How long to keep unused connections around(in seconds)
# Note: MySQL times out idle connections after 8hours(28,800seconds)
# so ensure this value is below MySQL idle timeout
cpool.maxIdleTime=25200
在 Spring 的配置文件中:

复制代码 代码如下:
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="maxIdleTime"value="${cpool.maxIdleTime}"/>
<!--other properties -->
</bean>

3. 定期使用连接池内的连接,使得它们不会因为闲置超时而被 MySQL 断开。
修改 c3p0 的配置文件,设置:

# Prevent MySQL raise exception after a long idle timecpool.preferredTestQuery='SELECT 1'cpool.idleConnectionTestPeriod=18000cpool.testConnectionOnCheckout=true
修改 Spring 的配置文件:

复制代码 代码如下:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="preferredTestQuery" value="${cpool.preferredTestQuery}"/>
<property name="idleConnectionTestPeriod" value="${cpool.idleConnectionTestPeriod}"/>
<property name="testConnectionOnCheckout" value="${cpool.testConnectionOnCheckout}"/>
<!--other properties --></bean> 
 

mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案的更多相关文章

  1. 【mysql】连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案

    1. 增加 MySQL 的 wait_timeout 属性的值.  修改 /etc/mysql/my.cnf文件,在 [mysqld] 节中设置: # Set a connection to wait ...

  2. mysql自动断开该连接解决方案

    mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案 作者: MySQL 的默认设置下,当一个连接的空闲时间超过8小时后,MySQL 就会断开该连接,而 c3p0 连接池则以为该被断 ...

  3. 解决数据库连接池连接mysql时,每隔8小时mysql自动断开所有连接的问题

    解决数据库连接池连接mysql时,每隔8小时mysql自动断开所有连接的问题 最近有个问题非常讨厌,我们的工程中使用自己的连接池连接mysql数据库,可mysql数据库每隔8小时就会自动断开所有链接, ...

  4. PLSQL 经常自动断开失去连接的解决过程

    转: PLSQL 经常自动断开失去连接的解决过程 问题背景: 情况是这样的,很多开发同事的PLSQL上班时间开着8个小时,有时候他们出去抽烟后或者中午吃完饭,回来在PLSQL上面执行就报错无响应,然后 ...

  5. PLSQL自动断开服务器连接 (转)

    最近,有同事反映在使用PLSQL连接数据库的时候,总是出现数据库频繁自动断开连接.这是因为资源配置文件中IDLE_TIME参数的限制,IDLE_TIME指定会话允许连续不活动的总的时间,以分钟为单位, ...

  6. mysql 5.1超过默认8小时空闲时间解决办法(错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure)

    报错: MySQL第二天早上第一次连接超时报错, com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications lin ...

  7. python 时间加8小时后的时间

    eta_temp = one['arrival'].encode('utf-8') fd = datetime.datetime.strptime(eta_temp, "%Y-%m-%dT% ...

  8. 关于联想笔记本不能连接无线网(wifi),注销后重新登录才可以连接

    解决联想笔记本wifi问题(果果) 最近很多使用联想的朋友都遇到了这样一个问题,那就是笔记本的wifi突然不能用了,好吧,其实我个人也遇到了这个问题,但是网上貌似对这个问题并没有给出一个可以解决的办法 ...

  9. Windows 远程连接后,自动断开,所有程序都自动关闭(待验证,待更新)

    win+r输入regedit打开注册表编辑SecurityLayer,将值改为2 计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Ter ...

随机推荐

  1. xshell中启动linux图形界面

    使用root用户执行xhost + IP为客户端机器IP,使用远程登录用户执行 DISPLAY=IP:0.0;export DISPLAY; 使用远程登录的用户执行: xhost +

  2. 《使用Win32DiskImager安装Ubuntu16.04》

    之前,一直使用的是“软碟通”来制作Ubuntu启动盘,或使用“EasyBCD”硬盘安装双系统,但前者在Ubuntu14.10及以后版本中便再也行不通了.此问题是由于Ubuntu使用了最新的syslin ...

  3. jquery trigger传值

    方法1: var e = $.Event('ok.menu.table', { relatedTarget: $el }) that.$el.trigger(e); 接受方法如下.此时,related ...

  4. Drupal 实战

    <Drupal 实战> 基本信息 作者: 葛红儒    丛书名: 实战系列 出版社:机械工业出版社 ISBN:9787111429999 上架时间:2013-6-28 出版日期:2013 ...

  5. java遍历map方法

    java 代码: import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Map ...

  6. ViewPager禁止滑动以及它与内层滑动控件水平方向上事件冲突的解决方法

    一.上图 二.场景描写叙述 最近在做项目的时候.遇到一个怪异的需求,描写叙述例如以下: 1.ViewPager中嵌套3个View,当从View1滑动到View2时禁止ViewPager的滑动事件. 2 ...

  7. [Unit Testing] Node testing: Test api Get request

    Using mocha: "devDependencies": { "should": "^5.2.0", "supertest& ...

  8. HashSet内存泄露

    import java.util.HashSet; import java.util.Set; public class PersonTest { public static void main(St ...

  9. Python 常量与变量

    先在lib文件夹中定义一个模块 class _const(object): class ConstError(TypeError):pass def __setattr__(self, name, v ...

  10. jquery ajax 跨域处理

    今天使用JQuery Ajax 在本地电脑获取远程服务器数据的时候,发现使用$.ajax,$.getJSON,$.get这些都没有反应,后来再统一个网站下测试了一下,代码写得没有问题.后来想了想好想, ...