【问题标题】:Can't connect to MySQL server at 127.0.0.1 using c3p0无法使用 c3p0 连接到位于 127.0.0.1 的 MySQL 服务器
【发布时间】:2015-07-23 04:48:25
【问题描述】:

尝试使用以下 Java 代码和 c3p0 连接到 MySQL 服务器:

comboPooledDataSource = new ComboPooledDataSource();  
comboPooledDataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/" + Dbname);  
comboPooledDataSource.setUser(Username);  
comboPooledDataSource.setPassword(Password);  
comboPooledDataSource.setInitialPoolSize(15);  
comboPooledDataSource.setMaxPoolSize(20);  
comboPooledDataSource.setMinPoolSize(10);  
connection = comboPooledDataSource.getConnection();

虽然我检查了数据库已启动并正在运行,并且我可以使用 Python 连接到它,但它一直向我抛出以下错误:

INFO: Initializing c3p0 pool...com.mchange.v2.c3p0.ComboPooledDataSource@b4c966a[ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, description -> null, driverClass -> null, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> b4c966a, idleConnectionTestPeriod -> -1, initialPoolSize -> 30, jdbcUrl -> jdbc:jtds:mysql://127.0.0.1:3306/sip_base, loginTimeout -> 0, maxIdleTime -> 0, maxPoolSize -> 20, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 10, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ]
Μαϊ 12, 2015 10:41:12 ΠΜ com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
WARNING: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@7e2889c7 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30).
Μαϊ 12, 2015 10:41:12 ΠΜ com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
WARNING: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@372567de -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30).
Exception in thread "main" java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:104)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:264)
at com.mchange.v2.c3p0.PoolBackedDataSource.getConnection(PoolBackedDataSource.java:94)
at com.mchange.v2.c3p0.ComboPooledDataSource.getConnection(ComboPooledDataSource.java:521)
at gov.nist.sip.proxy.extended.DBServer.<init>(DBServer.java:52)
at gov.nist.sip.proxy.extended.DBServer.getInstance(DBServer.java:64)
at gov.nist.sip.proxy.extended.DBServer.main(DBServer.java:161)
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.awaitAcquire(BasicResourcePool.java:972)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:208)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:260)
... 5 more

【问题讨论】:

  • 尝试降低初始池大小 - comboPooledDataSource.setInitialPoolSize(2);。至少让它低于你的maxPoolSize
  • connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/" + Dbname, Username, Password); 有效吗?如果没有,上面的 c3p0 代码也不会。请检查一下。还要在您的日志中查找描述上次尝试从 DBMS 获取连接失败的早期异常。

标签: java mysql database-connection c3p0


【解决方案1】:

您似乎试图将初始池大小设置为大于最大池大小 - 这不太可能奏效。

comboPooledDataSource.setInitialPoolSize(30);  // <<-- Reduce this 
comboPooledDataSource.setMaxPoolSize(20);  // <<-- to something less than this.

【讨论】:

    【解决方案2】:

    我已经解决了。原来我没有安装jdbc服务器。

    【讨论】:

      猜你喜欢
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多