I have a server that submits a query to a remote host to access account information from a database to log into a gameserver. The problem is the connections time out randomly however, I am using DataSource which should automatically re-establish any lost connections. Anyone have a clue how to resolve the issue?
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;
}
}