mysql 8小时问题
2016-04-04 21:16
896 查看
前段时间做项目的时候遇到了连接mysql数据每间隔一段时间不连接数据库,就会连接不上的问题,特别记录一下,这个问题是由于mysql存在一个8小时问题,默认情况下当连接空闲8个小时后,mysql数据库就会自动断开该连接,而这时我们的连接池还以为连接未断开可以正常使用,所以这个时候就会出现一个8小时问题,当遇到这个问题的时候我们有一下三种方案可以解决该问题:
1、增加 MySQL 的 wait_timeout 属性的值。
修改 /etc/mysql/my.cnf 文件,在 [mysqld] 节中设置:
# Set a connection to wait 8 hours in idle status.
wait_timeout = 86400
但是这个不能根除,不是很建议大家如此修改
2、减少连接池内连接的生存周期,使之小于上一项中所设置的 wait_timeout 的值。
修改 c3p0 的配置文件,设置:
# How long to keep unused connections around(in seconds)
# Note: MySQL times out idle connections after 8 hours(28,800 seconds)
# so ensure this value is below MySQL idle timeout
cpool.maxIdleTime=25200
在 Spring 的配置文件中:
[xhtml] view
plain copy
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="maxIdleTime" value="${cpool.maxIdleTime}" />
<!-- other properties -->
</bean>
3、定期使用连接池内的连接,使得它们不会因为闲置超时而被 MySQL 断开。
修改 c3p0 的配置文件,设置:
修改 Spring 的配置文件:
[xhtml] view plain copy <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、增加 MySQL 的 wait_timeout 属性的值。
修改 /etc/mysql/my.cnf 文件,在 [mysqld] 节中设置:
# Set a connection to wait 8 hours in idle status.
wait_timeout = 86400
但是这个不能根除,不是很建议大家如此修改
2、减少连接池内连接的生存周期,使之小于上一项中所设置的 wait_timeout 的值。
修改 c3p0 的配置文件,设置:
# How long to keep unused connections around(in seconds)
# Note: MySQL times out idle connections after 8 hours(28,800 seconds)
# so ensure this value is below MySQL idle timeout
cpool.maxIdleTime=25200
在 Spring 的配置文件中:
[xhtml] view
plain copy
<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 time cpool.preferredTestQuery='SELECT 1' cpool.idleConnectionTestPeriod=18000 cpool.testConnectionOnCheckout=true
修改 Spring 的配置文件:
[xhtml] view plain copy <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当前表使用的存储引擎(转)
- Mysql主从同步备份策略分享(实验修正版)
- mysql Install/Remove of the Service Denied!错误的解决办法
- mysql 一
- 解决 mysql 启动报错--发现系统错误2,系统找不到指定的文件
- MySQL数据库端字符集设置
- mysql 索引- 笔记
- MySQL数据库导入外部*.sql文件具体步骤
- 编译mysql 5.7 源码安装常见问题
- Mysql常用查询语句
- 吐槽 MySQL数据库jdbc操作,varchar类型占位符问题——单引号造孽
- MySQL 5.7 Distrib 5.7.8-rc 主从复制的简单配置,备忘
- 学习二:MySql数据库的基本操作
- 《MySQL必知必会学习笔记》组合查询
- MySQL数据库的CRUD操作
- MySQL数据库的连接
- MySQL数据库的连接
- mysql函数编写和存储过程
- mysql索引总结----mysql 索引类型以及创建
- mySQL