2014-10-29 115 views
4

我有一個java客戶端服務器,應該在啓動時建立連接池,但其超時錯誤失敗。上有同樣的問題這麼多線程,但沒有解決方案的使用JDK 7爲我工作java.sql.SQLException:客戶端嘗試檢出連接已超時

AM以下是mchange行家依賴

<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 [email protected] -- 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 

有人可以幫助我什麼是錯的,我的配置/代碼?

+0

您是否在另一個工具中測試了這些參數以驗證它們是否工作? – 2014-10-29 17:18:10

+0

是的,我能夠以相同的憑據登錄到SQL Server管理工作室,我也有其他的網絡應用程序,它運行良好,具有相同的jdbc屬性 – RanPaul 2014-10-29 17:19:43

+0

也許這是一個網絡問題,確保你啓動這個應用程序的服務器可以' ping「到數據庫服務器。 – 2014-10-29 17:21:02

回答

1

問題一般是

Transaction.Begin()不是直接或間接調用。 所以這可能是一個會話的問題,SessionFactory的或配置

你總是可以通過調試這個,做一個查詢您的會話對象,如果測試查詢工作事務,然後添加到它,並開始(),提交()適當地

相關問題