我使用下面的代码来获得Connection
。我已经使用c3p0
库进行连接池。如何在C3P0中配置连接存在检查?
package com.dataSource.c3p0;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DataSource {
private static DataSource datasource;
private ComboPooledDataSource cpds;
private DataSource() throws IOException, SQLException, PropertyVetoException {
cpds = new ComboPooledDataSource();
cpds.setDriverClass("com.mysql.jdbc.Driver"); //loads the jdbc driver
cpds.setJdbcUrl("jdbc:mysql://localhost/test");
cpds.setUser("root");
cpds.setPassword("root");
// the settings below are optional -- c3p0 can work with defaults
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);
cpds.setMaxStatements(180);
}
public static DataSource getInstance() throws IOException, SQLException, PropertyVetoException {
if (datasource == null) {
datasource = new DataSource();
return datasource;
} else {
return datasource;
}
}
public Connection getConnection() throws SQLException {
return this.cpds.getConnection();
}
}
现在我的问题是,这段代码没有检查连接是否存在。最有可能的是,这将在8小时后被着名的连接关闭错误所击中。
当我在休眠状态下使用C3P0时,有配置测试连接并重新建立连接。我做的配置是低于
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">3000</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1</property>
我怎么能在这里做结帐的同样的连接测试,所以我可以在JDBC中使用它呢?
PeakGen嗨,我面临着你提到的有关闭合连接错误的相同问题,但这种情况发生在相当长的时间之后而不是8小时之后。是否有任何配置设置,我可以检查以确认对我来说,它只是导致问题,因为我没有保持连接检查,如果它的有效或不。任何帮助真的很感激。谢谢 –