易截截图软件、单文件、免安装、纯绿色、仅160KB

使用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文档,


相关文档:

java 调用命令 备份mysql数据库

 代码如下:
  1  String command  =   " cmd /c C:/Program Files/MySQL/MySQL Server 5.0/bin>mysqldump -h localhost -u root -p aijia > E:/aijia.dmp " ;
  2      try    {
  3     Process process& ......

c基础

         张孝祥
另外,在实际的VC++教学中,发现很少有真正精通了C语言编程的学员,一般都有或多或少概念不是完全清楚的问题,特别是一些需要丰富的实战经验才能体会和明白的问题,如字符串,指针,类型转换,定义指向函数的指针类型,这也是导致学习VC++困难的一个原因。下面有 ......

约瑟夫循环(C编写)

#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 ......

mysql 设计遍历查询结果中的每行数据遇到的诸多问题

不同数据库之间的语法差异还真不少~要崩溃了。
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 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号