首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

c3p0 com.mysql.jdbc.CommunicationsException错误

2012-09-16 
c3p0 com.mysql.jdbc.CommunicationsException异常使用c3p0,偶尔会报异常:public synchronized static Dat

c3p0 com.mysql.jdbc.CommunicationsException异常

使用c3p0,偶尔会报异常:

public synchronized static DataSource createDataSource(String driver,String url,String username,String password) throws SQLException, ClassNotFoundException {Class.forName(driver);DataSource ds_unpooled = DataSources.unpooledDataSource(url,username,password); Map overrides = new HashMap(); //当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 overrides.put("acquireIncrement", 5);overrides.put("minPoolSize", 5);overrides.put("maxPoolSize", 10);//overrides.put("initialPoolSize",cfg.getMaxPoolSize());overrides.put("maxStatements", 10000);//最大空闲时间,3600秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0overrides.put("maxIdleTime", 3600 );overrides.put("automaticTestTable", "C3P0TestTable");overrides.put("testConnectionOnCheckin", true);//每60秒检查所有连接池中的空闲连接。Default: 0overrides.put("idleConnectionTestPeriod",18000);overrides.put("testConnectionOnCheckout", true);//获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效 //保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试 //获取连接失败后该数据源将申明已断开并永久关闭。Default: false overrides.put("breakAfterAcquireFailure", true);//c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能 //通过多线程实现多个操作同时被执行。Default: 3overrides.put("numHelperThreads", 10);// create the PooledDataSource using the default configuration and our overridesDataSource ds_pooled = DataSources.pooledDataSource( ds_unpooled, overrides );return ds_pooled; }

?

主要加了参数:

//如果设为true那么在取得连接的同时将校验连接的有效性。Default: false
testConnectionOnCheckin = true
//自动测试的table名称,c3p0将建一张名为C3P0TestTable的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么
//属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试使用。Default: null
automaticTestTable=C3P0TestTable
//每1800秒检查所有连接池中的空闲连接。Default: 0
idleConnectionTestPeriod = 1800
//最大空闲时间,3600秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0
maxIdleTime = 3600
/**因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
等方法来提升连接测试的性能。Default: false */
testConnectionOnCheckout = true

另外:还在JDBC URL上面加一个autoReconnect=true 参数

而且mysql服务的wait_timeout和interactive_timeout的值都是172800,已经远远大于c3p0的maxIdleTime参数值

做了这些修改后,还是会报错,而且平均每个小时有8-10次这样的错误发生。不知道还有没有更好的解决办法?谢谢。

还有种可能,mysql承受不了太大压力时是不是也会报这种错误呢?

1 楼 jeho0815 2011-01-05   为什么没有人在这回答这个问题呢?。。。我一个哥们也出现了这个情况,搞了两个礼拜也没搞定,也不着调原因是什么,希望有牛人来搞定指导下哈!

热点排行