我使用下面的代碼來獲得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小時之後。是否有任何配置設置,我可以檢查以確認對我來說,它只是導致問題,因爲我沒有保持連接檢查,如果它的有效或不。任何幫助真的很感激。謝謝 –