cjar包:c3p0-0.9.5-pre6.jar mchange-commons-java-0.2.6.3.jar
背景:以前在使用ORM框架的情况下,使用连接池没出现过空闲连接超时的问题,现在新建个项目没什么框架,使用c3p0,代码如下:
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;
}
有个线程用 getConn()获取连接后,长时间不进行数据库操作,隔天触发数据库操作时会报异常
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)
有个线程一直有进行数据库操作,则正常,因此判断是MYSQL8小时问题。但是代码里几个重要属性已经配置了,是否哪里还漏了,错了。
求教!
--转自