【问题标题】:java.sql.SQLException: An attempt by a client to checkout a Connection has timed outjava.sql.SQLException:客户端检出连接的尝试已超时
【发布时间】:2014-10-29 17:16:29
【问题描述】:

我有一个 java 客户端服务器,它应该在启动时建立连接池,但它在超时错误时失败。关于同一个问题有很多线程,但没有一个解决方案对我有用

AM 使用 jdk 7 及以下版本是 mchange maven 依赖

<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.2.1</version>
</dependency>

jdbc.properties

url=jdbc\:sqlserver\://server\\instance;databaseName\=db
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
user=admin
password=admin
maxPoolSize=20
minPoolSize=5
acquireIncrement=5

这是我的 DAO 类,它在服务器启动时建立连接池

public class ShareDocDAO {
//..........
 private static Connection getConnection() throws SQLException {
        LOG.info("Getting DB connection");
        ComboPooledDataSource cpds = getPool();

        return cpds.getConnection(); //Line 36:
    }
 private static ComboPooledDataSource getPool() {
        if (pool!=null) {
            return pool;
        }

        ComboPooledDataSource cpds = new ComboPooledDataSource();
        try {
            Properties dbProperties = getDbProperties();

            //loads the jdbc driver
            cpds.setDriverClass(dbProperties.getProperty("driver"));             
            cpds.setJdbcUrl(dbProperties.getProperty("url"));
            cpds.setUser(dbProperties.getProperty("user"));
            cpds.setPassword(dbProperties.getProperty("password"));

            // the settings below are optional -- c3p0 can work with defaults
            cpds.setMinPoolSize(Integer.valueOf(dbProperties.getProperty("minPoolSize")));
            cpds.setAcquireIncrement(Integer.valueOf(dbProperties.getProperty("acquireIncrement")));
            cpds.setMaxPoolSize(Integer.valueOf(dbProperties.getProperty("maxPoolSize")));
            cpds.setCheckoutTimeout(30000);
            cpds.setIdleConnectionTestPeriod(10800);
            cpds.setMaxIdleTime(21600);
            LOG.info("cpds driver "+cpds.getDriverClass()+ " JDBC URL = "+cpds.getJdbcUrl() +" User = "+cpds.getUser()+ " Pwd = "+cpds.getPassword()+ " MinPoolSize "+cpds.getMinPoolSize() +" AcquireIncrement "+cpds.getAcquireIncrement() +" MaxPoolSize "+cpds.getMaxPoolSize());
        } catch (Exception ex) {
            LOG.error("failed to create pool", ex);
        }
        pool = cpds;
        return pool;
    }
}

这是堆栈跟踪

[Oct 29 11:58:22] SSLHandshake-3 | ERROR | com.dc.ssltunnel.server.ShareDocDAO | Error querying for User Information
java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:77)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:687)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
    at com.dc.ssltunnel.server.ShareDocDAO.getConnection(ShareDocDAO.java:36)
    at com.dc.ssltunnel.server.ShareDocDAO.checkSerialNumber(ShareDocDAO.java:75)
    at com.dc.ssltunnel.server.MainServer.retrieveOrCreateClient(MainServer.java:95)
    at com.dc.ssltunnel.server.MainServerHandshakeThread.handshake(MainServerHandshakeThread.java:58)
    at com.dc.ssltunnel.server.MainServerHandshakeThread.run(MainServerHandshakeThread.java:71)
    at com.dc.ssltunnel.utilities.threading.ShutdownThreadPoolExecutor$1.run(ShutdownThreadPoolExecutor.java:36)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: com.mchange.v2.resourcepool.TimeoutException: A client timed out while waiting to acquire a resource from com.mchange.v2.resourcepool.BasicResourcePool@3414b7f8 -- timeout at awaitAvailable()
    at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1416)
    at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:606)
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:526)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:755)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:682)
    ... 12 more
[Oct 29 11:58:22] SSLHandshake-3 | DEBUG | com.dc.ssltunnel.server.MainServerHandshakeThread | Handshake thread is done
[Oct 29 11:58:22] SSLHandshake-3 | DEBUG | com.dc.ssltunnel.server.MainServerHandshakeThread | Handshake thread is done
[Oct 29 11:58:22] SSLHandshake-1 | INFO  | com.dc.ssltunnel.server.ShareDocDAO | Getting DB connection
[Oct 29 11:58:22] SSLHandshake-1 | INFO  | com.dc.ssltunnel.server.ShareDocDAO | Getting DB connection
[Oct 29 11:58:22] SSLHandshake-3 | DEBUG | com.dc.ssltunnel.utilities.threading.ShutdownThreadPoolExecutor | RUNNABLE:removing total:2
[Oct 29 11:58:22] SSLHandshake-3 | DEBUG | com.dc.ssltunnel.utilities.threading.ShutdownThreadPoolExecutor | RUNNABLE:removing total:2
[Oct 29 11:58:22] SSLHandshake-1 | ERROR | com.dc.ssltunnel.server.ShareDocDAO | Error querying for User Information
java.sql.SQLException: Connections could not be acquired from the underlying database!
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:689)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
    at com.dc.ssltunnel.server.ShareDocDAO.getConnection(ShareDocDAO.java:36)
    at com.dc.ssltunnel.server.ShareDocDAO.checkSerialNumber(ShareDocDAO.java:75)
    at com.dc.ssltunnel.server.MainServer.retrieveOrCreateClient(MainServer.java:95)
    at com.dc.ssltunnel.server.MainServerHandshakeThread.handshake(MainServerHandshakeThread.java:58)
    at com.dc.ssltunnel.server.MainServerHandshakeThread.run(MainServerHandshakeThread.java:71)
    at com.dc.ssltunnel.utilities.threading.ShutdownThreadPoolExecutor$1.run(ShutdownThreadPoolExecutor.java:36)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
    at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1418)
    at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:606)
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:526)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:755)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:682)
    ... 12 more

有人可以帮我看看我的配置/代码有什么问题吗?

【问题讨论】:

  • 您是否在其他工具中测试过这些参数以验证它们是否有效?
  • 是的,我能够使用相同的凭据登录到 sql server management studio,而且我还有其他使用相同 jdbc 属性运行良好的 Web 应用程序
  • 可能是网络问题,确保你启动这个应用的服务器可以ping到数据库服务器。
  • 我可以 ping 服务器和我的其他具有相同 jdbc 属性的 Web 应用程序工作正常
  • 嘿,你有没有找到解决方案?面临同样的问题

标签: java sql-server connection-pooling spring-jdbc


【解决方案1】:

问题一般

Transaction.Begin() 不会直接或间接调用。 所以这可能是会话、SessionFactory 或配置的问题

您始终可以通过使用会话对象进行查询来调试它,如果该测试查询有效,则向其添加事务并适当地开始(),提交()

【讨论】:

    【解决方案2】:

    在某些情况下,开发人员会尝试在特定的开发环境中连接本地/远程数据库。所以请检查所需的数据库实例是否已启动/可用,或者您是否有权访问它?在这种情况下例外:

    Could not get JDBC Connection; nested exception is java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
    

    可能发生。

    #connection-pooling #sql-server #spring-jdbc #java

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      相关资源
      最近更新 更多