如何在 C3P0 中配置连接存在性检查?
How configure connection existence check in C3P0?
我正在使用以下代码获取 Connection
。我已将 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 中使用它?
configuring connection testing in the C3P0 documentation. Also, see the section on configuration override 有一个很好的部分,特别是优先规则。
(特别是,关于使用支持isValid()
的JDBC4驱动的建议很好。)
在您的代码中,您只需添加
cpds.setTestConnectionOnCheckout( true );
cpds.setPreferredTestQuery( "SELECT 1" );
到您的 DataSource
构造函数。确保查询实际上与数据库对话,我似乎记得一些驱动程序实现实际上并没有针对此类某些查询进入数据库 :-)
干杯,
我正在使用以下代码获取 Connection
。我已将 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 中使用它?
configuring connection testing in the C3P0 documentation. Also, see the section on configuration override 有一个很好的部分,特别是优先规则。
(特别是,关于使用支持isValid()
的JDBC4驱动的建议很好。)
在您的代码中,您只需添加
cpds.setTestConnectionOnCheckout( true );
cpds.setPreferredTestQuery( "SELECT 1" );
到您的 DataSource
构造函数。确保查询实际上与数据库对话,我似乎记得一些驱动程序实现实际上并没有针对此类某些查询进入数据库 :-)
干杯,