使用C3P0连接Mysql的一个问题的解决过程
最近的一个项目在使用C3P0的连接池,数据库为Mysql。开发测试没有问题,在运行中每个一段长的空闲时间就出现异常Communications link failure due to underlying exception:
java 代码
查看了Mysql的文档,以及Connector/J的文档以及在线说明发现,出现这种异常的原因是:
Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection。这就是问题的所在,在C3P0 pools中的connections如果空闲超过8小时,Mysql将其断开,而C3P0并不知道该connection已经失效,如果这时有Client请求connection,C3P0将该失效的Connection提供给Client,将会造成上面的异常。
解决的方法有3种:
增加wait_timeout的时间。
减少Connection pools中connection的lifetime。
测试Connection pools中connection的有效性。
当然最好的办法是同时综合使用上述3种方法,假设wait_timeout为默认的8小时
C3P0增加以下配置信息:
//获取connnection时测试是否有效
testConnectionOnCheckin = true
//自动测试的table名称
automaticTestTable=C3P0TestTable
//set to something much less than wait_timeout, prevents connections from going stale
idleConnectionTestPeriod = 18000
//set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out
maxIdleTime = 25000
//if you can take the performance 'hit', set to "true"
testConnectionOnCheckout = true
更多的配置信息大家可以查看C3P0文档,Connector/J文档,
相关文档:
#include<stdio.h>
const int A=10;
void main()
{
int group[A];
for(int i=0;i<A;i++)
{
group[i]=i+1;
}
int k=A;
int m=0;
int n=2;
int c=0;
int x;
do
{
&nbs ......
简述
一 列文件清单
二:执行程序
三:显示数据
四:断点(breakpoint)
五.断点的管理
六.变量的检查和赋值
七. 单步执行
八.函数的调用
&nb ......
最近因为研究一个东西,并且在很久以前就想把python好好看看。正好满足我的好奇心。我每天上班做得游戏都是用lua,也是一门很强大的脚本语言。可能我更喜欢python的缩进区分和面向对象吧。 今天只写一个简单的例子。一个简单的python脚本,就一个函数,用C/C++去调用。可能这也是作为程序来说最关心的一件事。所以我的pytho ......
不同数据库之间的语法差异还真不少~要崩溃了。
1.创建带自增列临时表:create TEMPORARY table t(id int AUTO_INCREMENT PRIMARY key, u_name varchar(50));--为什么必须要主键?!
向这个临时表中插入数据:insert into t values(null, 'zdy');--为什么一定要加null?!
2.把一张表的查询结果插入临时表:insert into t ......