简体   繁体   中英

SQL connections timing out with DataSource

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;
}
}

"Back in the day...", in 2003, I was working with Tomcat 4.1 and was unpleasantly surprised to find that its implementation of DataSource required you to give it a validationQuery or else it would initialize the Connection only once and never verify that database server side would still respect the connection. Our Oracle server would simply eliminate connections that hadn't been used for, I think, 60 minutes. The effect of this was that as my server went to low volume, some number of my pooled connections would get killed on the database server side, but the application server didn't know, until it tried to use them at higher volumes. At this point they just stacktraced and that was the end of it. Adding the validationQuery had the effect of the DataSource itself keeping the connection alive and everything just worked. The gory details of this discovery process is here https://groups.yahoo.com/neo/groups/seajug/conversations/messages/4902 , if you are interested.

I recommend that you check if your GenericObjectPool implementation has a concept of validation query or heartbeat or something and figure out how to leverage it to keep your connections "fresh".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM