0 0

c3p0关于mysql8小时的问题5

jar包:c3p0-0.9.5-pre6.jar     mchange-commons-java-0.2.6.3.jar
背景:以前在使用ORM框架的情况下,使用连接池没出现过空闲连接超时的问题,现在新建个项目没什么框架,使用c3p0,代码如下:
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;
  }


有个线程用 getConn()获取连接后,长时间不进行数据库操作,隔天触发数据库操作时会报异常
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)

有个线程一直有进行数据库操作,则正常,因此判断是MYSQL8小时问题。但是代码里几个重要属性已经配置了,是否哪里还漏了,错了。
求教!
2014年2月21日 09:18

3个答案 按时间排序 按投票排序

0 0

采纳的答案

2014年2月21日 10:21
0 0

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

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

3、建议使用proxool连接池。

2014年2月21日 16:16
0 0

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

2014年2月21日 16:11

相关推荐

Global site tag (gtag.js) - Google Analytics