简体   繁体   English

SQL连接使用DataSource超时

[英]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. 问题是连接随机超时,但是,我正在使用DataSource,它应该自动重新建立任何丢失的连接。 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. “回想起来……”,在2003年,我正在使用Tomcat 4.1,感到惊讶的是,它的DataSource实现要求您给它提供一个ValidationQuery,否则它将只初始化一次Connection,并且从不验证数据库服务器端仍然会尊重连接。 Our Oracle server would simply eliminate connections that hadn't been used for, I think, 60 minutes. 我们的Oracle服务器将简单地消除60分钟未使用的连接。 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. 添加validationQuery具有DataSource本身的作用,可以保持连接有效,并且一切正常。 The gory details of this discovery process is here https://groups.yahoo.com/neo/groups/seajug/conversations/messages/4902 , if you are interested. 如果您有兴趣,请参见https://groups.yahoo.com/neo/groups/seajug/conversations/messages/4902 ,了解此发现过程的详细信息。

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". 我建议您检查您的GenericObjectPool实现是否具有验证查询或心跳等概念,并弄清楚如何利用它来保持“新鲜”连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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