使用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文档,
相关文档:
[原创] MySQL Proxy 学习笔记
作者:heiyeluren
时间:2008-1-28
博客:http://blog.csdn.net/heiyeshuwu
【 测试平台 】
服务器端:
OS:FreeBSD 6.2
Lua: 5.1.1
MySQL Server:4.1.22-log
MySQL Proxy: 0.6.0
客户端:
OS:Windows XP Pro SP2
PHP: PHP 5.2.4 (cli)
MySQL Client: Ver 14.13 Distrib ......
#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 ......
今天在douban上面看到了一个帖子,里面关于乘法问题大家讨论了一下
http://www.douban.com/group/topic/8384097/
看到移位做乘法也不是第一次了,但是很诧异真的会在用,自己水平还就差了那么一点点,无可否认,我们处理器的ALU做移位是相当高效的。
这里记录一下里面的一个移位乘法例子。
#include <stdio.h>
# ......
不同数据库之间的语法差异还真不少~要崩溃了。
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 ......