我有一臺服務器向遠程主機提交查詢,以便從數據庫訪問帳戶信息以登錄到遊戲服務器。問題是連接超時,但我正在使用DataSource應該自動重建任何丟失的連接。任何人都有線索如何解決這個問題?SQL連接與DataSource超時
public class Database {
private final PoolingDataSource source;
public Database(String driver, String url, String user, String password)
throws ClassNotFoundException {
Class.forName(driver);
source = createSource(url, user, password);
}
@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
private PoolingDataSource createSource(String connectURI, String user,
String password) {
GenericObjectPool.Config config = new GenericObjectPool.Config();
config.maxActive = 150;
config.maxIdle = 100;
config.minIdle = 30;
config.maxWait = 1000;
config.testWhileIdle = true;
ObjectPool connectionPool = new GenericObjectPool(null, config);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
connectURI, user, password);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
connectionFactory, connectionPool, null, null, false, true);
PoolingDataSource poolingDataSource = new PoolingDataSource(
connectionPool);
return poolingDataSource;
}
public Connection getConnection() throws SQLException {
return getSource().getConnection();
}
public PoolingDataSource getSource() {
return source;
}
}
某些池數據源使用收割線程來回收看似未使用的連接。你是否堅持這些連接,並且他們是否有長時間運行的查詢? –
不,我不是,但是我應該糾正我原來的帖子。提交查詢時連接只會超時。 – Hoodlom