在连接字符串中  添加设置节点 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> 
 

最新文章

  1. 如何在Linux中搭建禅道8.4.1(httpd+php+mysql)
  2. ubuntu VNC server 黑屏 yum源更新(ubuntu16.04)
  3. phpstorm的安装和破解
  4. OpenJudge计算概论-字符串最大跨距
  5. 总结nonatomic,assigncopy,retain
  6. oracle rac IP详解
  7. CSS Sprites的详细使用步骤
  8. 【Alpha】Daily Scrum Meeting——Day5
  9. iOS隐藏导航栏底部灰线
  10. SpringCloud使用Nacos服务发现实现远程调用
  11. Spring Boot + Mybatis + Redis二级缓存开发指南
  12. js使用锚点回到顶部
  13. [ISE 14.7]Fail to Link the designer导致无法仿真问题
  14. bzoj5068: 友好的生物
  15. C# 文件上传 制作水印
  16. 总结开发ERP软件应遵循的一些基本原则
  17. SQL Server更改排序规则的实现过程
  18. android游戏的增量更新(资源及代码的热更新)
  19. HH实习 acm算法部 1689
  20. 【BZOJ 1115】【POI 2009】石子游戏Kam

热门文章

  1. 如何将HashMap,按照value值排序
  2. [LeetCode] Word Search [37]
  3. POJ2230 Watchcow【欧拉回路】
  4. 如果ASM磁盘组由哪些物理磁盘组成?
  5. JQuery window、document、 body (转)
  6. MyEclipse设置默认的文档注释
  7. css过渡+3D
  8. javascript高级特性(面向对象)
  9. 营配数据质量核查,关于营销mis系统与配电gis系统里面的sql语句查询,做为积累使用,下次就不用重复写同样的语句了。
  10. MYSQL Model报错:指定的存储区提供程序在配置中找不到 的解决