xblueskyleng
2014-02-21 09:18 阅读 319

c3p0关于mysql8小时的问题

jar包:c3p0-0.9.5-pre6.jar mchange-commons-java-0.2.6.3.jar
背景:以前在使用ORM框架的情况下,使用连接池没出现过空闲连接超时的问题,现在新建个项目没什么框架,使用c3p0,代码如下:
[code="java"]
private static ComboPooledDataSource ds = null;

static {
try {
ds = new ComboPooledDataSource();
ds.setDriverClass(Constant.DRIVER_CLASS_NAME);
ds.setJdbcUrl(Constant.URL);
ds.setUser(Constant.USERNAME);
ds.setPassword(Constant.PASSWORD);
ds.setMaxPoolSize(40);
ds.setMinPoolSize(5);
ds.setAutomaticTestTable("C3P0TestTable");
ds.setIdleConnectionTestPeriod(1800);
ds.setTestConnectionOnCheckin(true);
ds.setTestConnectionOnCheckout(true);
ds.setMaxIdleTime(25000);
} catch (PropertyVetoException e) {
gLog.error("ComboPooledDataSource", e);
}
}

public static synchronized Connection getConn() {
Connection con = null;
try {
con = ds.getConnection();
} catch (SQLException e1) {
gLog.error("getConn", e1);
}
return con;
}

[/code]
[color=red]有个线程用 getConn()获取连接后,长时间不进行数据库操作,隔天触发数据库操作时会报异常[/color]
[code="java"]
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: Software caused connection abort: socket write error

STACKTRACE:

java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2637)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1554)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3176)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1153)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1266)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:144)

** END NESTED EXCEPTION **

Last packet sent to the server was 0 ms ago.
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2652)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1554)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3176)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1153)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1266)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:144)
[/code]
有个线程一直有进行数据库操作,则正常,因此判断是MYSQL8小时问题。但是代码里几个重要属性已经配置了,是否哪里还漏了,错了。
求教!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    zyn010101 zyn010101 2014-02-21 10:21
    点赞 评论 复制链接分享
  • chenchu123 潜伏的猫 2014-02-21 16:11

    c3p0应该有闲置多少时间把连接返库的配置吧

    点赞 评论 复制链接分享
  • xupei1317_java xupei1317_java 2014-02-21 16:16

    1、修改数据库配置
    interactive_timeout=?
    wait_timeout=?

    2、c3p0在断开连接时,不能自动重连,所以不能保证连接池有连接。

    3、建议使用proxool连接池。

    点赞 评论 复制链接分享

相关推荐