c3p0 com.mysql.jdbc.CommunicationsException异常
来源:互联网 发布:游戏鸡充值软件 编辑:程序博客网 时间:2024/05/17 01:41
使用c3p0,偶尔会报异常:
出现这种异常的原因是:
Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection。这就是问题的所在,在C3P0 pools中的connections如果空闲超过8小时,Mysql将其断开,而C3P0并不知道该connection已经失效,如果这时有Client请求connection,C3P0将该失效的Connection提供给Client,将会造成上面的异常。
解决办法:
注意参数:
//如果设为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的值要大于c3p0的maxIdleTime参数值
- 2008-02-18 10:52:53 ERROR Thread-4 com.feedsky.dao.AbstractMutiDbDAO - com.mysql.jdbc.CommunicationsException: Communications
- link failure due to underlying exception:
- ** BEGIN NESTED EXCEPTION **
- java.io.EOFException
- STACKTRACE:
- java.io.EOFException
- at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1913)
- at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2304)
- at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803)
- at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
- at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
- at com.mysql.jdbc.Connection.execSQL(Connection.java:3170)
- at com.mysql.jdbc.Connection.execSQL(Connection.java:3099)
- at com.mysql.jdbc.Statement.executeQuery(Statement.java:1169)
- at com.mchange.v2.c3p0.impl.NewProxyStatement.executeQuery(NewProxyStatement.java:35)
- at com.feedsky.dao.AbstractMutiDbDAO.findBySQL(AbstractMutiDbDAO.java:34)
- at com.feedsky.dao.impl.SrcItemDAOImpl.findBySQL(SrcItemDAOImpl.java:326)
- at com.feedsky.dao.impl.SrcItemDAOImpl.loadByItemUuid(SrcItemDAOImpl.java:255)
- at com.feedsky.service.impl.SrcItemServiceImpl.loadByItemUuid(SrcItemServiceImpl.java:37)
- at com.feedsky.controller.SrcFeedController.saveSrcFeed(SrcFeedController.java:97)
- at com.feedsky.work.SrcStoreThread.saveSrcFeed(SrcStoreThread.java:174)
- at com.feedsky.work.SrcStoreThread.run(SrcStoreThread.java:53)
- at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:748)
- at java.lang.Thread.run(Thread.java:619)
- ** END NESTED EXCEPTION **
- Last packet sent to the server was 176 ms ago.
- 2008-02-18 10:52:53 ERROR Thread-4 com.feedsky.work.SrcStoreThread - java.lang.NullPointerException
- 2008-02-18 10:52:53 INFO Thread-2 com.feedsky.dao.impl.SrcItemDAOImpl - com.mysql.jdbc.CommunicationsException:
- Communications link failure due to underlying exception:
- ** BEGIN NESTED EXCEPTION **
- java.io.EOFException
- STACKTRACE:
- java.io.EOFException
- at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1913)
- at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2304)
- at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803)
- at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
- at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
- at com.mysql.jdbc.Connection.execSQL(Connection.java:3170)
- at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1316)
- at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1235)
- at com.mchange.v2.c3p0.impl.NewProxyStatement.executeUpdate(NewProxyStatement.java:64)
- at com.feedsky.dao.impl.SrcItemDAOImpl.insert(SrcItemDAOImpl.java:178)
- at com.feedsky.service.impl.SrcItemServiceImpl.save(SrcItemServiceImpl.java:29)
- at com.feedsky.controller.SrcFeedController.saveSrcFeed(SrcFeedController.java:111)
- at com.feedsky.work.SrcStoreThread.saveSrcFeed(SrcStoreThread.java:174)
- at com.feedsky.work.SrcStoreThread.run(SrcStoreThread.java:53)
- at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:748)
- at java.lang.Thread.run(Thread.java:619)
- ** END NESTED EXCEPTION **
2008-02-18 10:52:53 ERROR Thread-4 com.feedsky.dao.AbstractMutiDbDAO - com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.io.EOFExceptionSTACKTRACE:java.io.EOFException at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1913) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2304) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665) at com.mysql.jdbc.Connection.execSQL(Connection.java:3170) at com.mysql.jdbc.Connection.execSQL(Connection.java:3099) at com.mysql.jdbc.Statement.executeQuery(Statement.java:1169) at com.mchange.v2.c3p0.impl.NewProxyStatement.executeQuery(NewProxyStatement.java:35) at com.feedsky.dao.AbstractMutiDbDAO.findBySQL(AbstractMutiDbDAO.java:34) at com.feedsky.dao.impl.SrcItemDAOImpl.findBySQL(SrcItemDAOImpl.java:326) at com.feedsky.dao.impl.SrcItemDAOImpl.loadByItemUuid(SrcItemDAOImpl.java:255) at com.feedsky.service.impl.SrcItemServiceImpl.loadByItemUuid(SrcItemServiceImpl.java:37) at com.feedsky.controller.SrcFeedController.saveSrcFeed(SrcFeedController.java:97) at com.feedsky.work.SrcStoreThread.saveSrcFeed(SrcStoreThread.java:174) at com.feedsky.work.SrcStoreThread.run(SrcStoreThread.java:53) at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:748) at java.lang.Thread.run(Thread.java:619)** END NESTED EXCEPTION **Last packet sent to the server was 176 ms ago.2008-02-18 10:52:53 ERROR Thread-4 com.feedsky.work.SrcStoreThread - java.lang.NullPointerException2008-02-18 10:52:53 INFO Thread-2 com.feedsky.dao.impl.SrcItemDAOImpl - com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.io.EOFExceptionSTACKTRACE:java.io.EOFException at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1913) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2304) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665) at com.mysql.jdbc.Connection.execSQL(Connection.java:3170) at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1316) at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1235) at com.mchange.v2.c3p0.impl.NewProxyStatement.executeUpdate(NewProxyStatement.java:64) at com.feedsky.dao.impl.SrcItemDAOImpl.insert(SrcItemDAOImpl.java:178) at com.feedsky.service.impl.SrcItemServiceImpl.save(SrcItemServiceImpl.java:29) at com.feedsky.controller.SrcFeedController.saveSrcFeed(SrcFeedController.java:111) at com.feedsky.work.SrcStoreThread.saveSrcFeed(SrcStoreThread.java:174) at com.feedsky.work.SrcStoreThread.run(SrcStoreThread.java:53) at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:748) at java.lang.Thread.run(Thread.java:619)** END NESTED EXCEPTION **
出现这种异常的原因是:
Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection。这就是问题的所在,在C3P0 pools中的connections如果空闲超过8小时,Mysql将其断开,而C3P0并不知道该connection已经失效,如果这时有Client请求connection,C3P0将该失效的Connection提供给Client,将会造成上面的异常。
解决办法:
- 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: 0
- overrides.put("maxIdleTime", 3600 );
- overrides.put("automaticTestTable", "C3P0TestTable");
- overrides.put("testConnectionOnCheckin", true);
- // 每60秒检查所有连接池中的空闲连接。Default: 0
- overrides.put("idleConnectionTestPeriod",18000);
- overrides.put("testConnectionOnCheckout", true);
- //获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
- //保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
- //获取连接失败后该数据源将申明已断开并永久关闭。Default: false
- overrides.put("breakAfterAcquireFailure", true);
- //c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能
- //通过多线程实现多个操作同时被执行。Default: 3
- overrides.put("numHelperThreads", 10);
- // create the PooledDataSource using the default configuration and our overrides
- DataSource ds_pooled = DataSources.pooledDataSource( ds_unpooled, overrides );
- return ds_pooled;
- }
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的值要大于c3p0的maxIdleTime参数值
- c3p0 com.mysql.jdbc.CommunicationsException异常
- c3p0 com.mysql.jdbc.CommunicationsException异常
- c3p0 com.mysql.jdbc.CommunicationsException异常
- c3p0 com.mysql.jdbc.CommunicationsException
- Mysql异常com.mysql.jdbc.exceptions.jdbc4.CommunicationsException..CommunicationsException: Communicati
- java链接Mysql异常:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
- Mysql异常:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
- Mysql异常com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications
- 数据库连接池与com.mysql.jdbc.exceptions.jdbc4.CommunicationsException异常
- Mysql autoReconnect 问题com.mysql.jdbc.CommunicationsException
- com.mysql.jdbc.CommunicationsException: Communications link failure
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
- mysql的异常:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
- 异常解决:Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
- 常见异常整理之一:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The la
- mysql 数据库问题com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
- mysql连接com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link
- java教程:解析java的多线程机制(二)
- java教程之awt图形用户界面设计(二)
- 数据库连接小例子
- SSRS报表开发
- [手把手教]【整理一些discuz插件开发制作入门级教程】
- c3p0 com.mysql.jdbc.CommunicationsException异常
- 错误记录--Sql proc
- 简单驱动模块Makefile解析
- 关于Struts2 上传文件获取文件名称和类型(标准做法)
- 安装awstats和配置tomcat
- 将RDL嵌入应用程序
- 通过5v自举升压到15v——————————————————————(转自百度文库)
- android修改键值
- [转]一个Java项目的标准开发流程