尝试连接 docker mysql 时,ResourcePool 无法从其主工厂或源错误获取资源

Posted

技术标签:

【中文标题】尝试连接 docker mysql 时,ResourcePool 无法从其主工厂或源错误获取资源【英文标题】:A ResourcePool could not acquire a resource from its primary factory or source error when try to connect docker mysql 【发布时间】:2018-11-09 01:33:28 【问题描述】:

,

我正在尝试将 docker mysql 与休眠应用程序一起使用。 我的 db.properties 是:

hibernate.connection.username=root
hibernate.connection.password=pass
hibernate.hbm2ddl.auto=update
hibernate.connection.url=jdbc:mysql://172.17.0.2:3306/keystone?autoReconnect=true
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

使用 docker 容器 ip (172.17.0.2) 的位置。

我的休眠配置是:

    <property name="show_sql">false</property>
    <property name="format_sql">true</property>
    <property name="use_sql_comments">false</property>
    <property name="generate_statistics">false</property>
    <property name="max_fetch_depth">2</property>
    <property name="default_batch_fetch_size">16</property>
    <property name="use_streams_for_binary">true</property>
    <property name="connection.isolation">2</property>
    <property name="hibernate.transaction.flush_before_completion">true</property>
    <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>

    <property name="hibernate.c3p0.min_size">2</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">300</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>
    <property name="hibernate.cache.use_structured_entries">true</property>
    <property name="hibernate.cache.use_query_cache">true</property>
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

</session-factory>

我可以使用任何客户端访问 docker mysql,但是当我尝试从我的应用程序连接时,我收到此错误:

 <exception name="Could not open connection">
    org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)
    at org.jpos.ee.DB.beginTransaction(DB.java:168)
    at com.bkash.keystone.participant.AbstractParticipant.openDB(AbstractParticipant.java:91)
    at com.bkash.keystone.participant.AbstractParticipant.createTrace(AbstractParticipant.java:150)
    at com.bkash.keystone.participant.AbstractParticipant.traceDb(AbstractParticipant.java:144)
    at com.bkash.keystone.participant.AbstractParticipant.log(AbstractParticipant.java:124)
    at com.bkash.keystone.participant.AbstractParticipant.prepare(AbstractParticipant.java:68)
    at org.jpos.transaction.TransactionManager.prepare(TransactionManager.java:571)
    at org.jpos.transaction.TransactionManager.prepare(TransactionManager.java:637)
    at org.jpos.transaction.TransactionManager.run(TransactionManager.java:308)
    at java.lang.Thread.run(Thread.java:748)
Caused by: 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 org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:84)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
    ... 14 more
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)
    ... 18 more
    </exception>

Hibernate 无法从 mysql 连接池获取连接。不知道为什么。

有人可以帮忙吗?

【问题讨论】:

【参考方案1】:

我已经找到了这个问题的解决方案。我已经使用了以下命令。

sudo docker run --name keystone -e MYSQL_ROOT_PASSWORD=pass -e MYSQL_DATABASE=keystone -d mysql:最新

其中安装了mysql verison 8.0.11和Authentication Type 'caching_sha2_password' 是问题所在。 这个answer对我帮助很大。

我已经使用以下命令创建了容器 (mysql:5.7)

sudo docker run --name keystone -e MYSQL_ROOT_PASSWORD=pass -e MYSQL_DATABASE=keystone -d mysql:5.7

这允许我创建具有标准身份验证类型的用户。

注意不要忘记添加必需的管理角色Schema Privileged

【讨论】:

以上是关于尝试连接 docker mysql 时,ResourcePool 无法从其主工厂或源错误获取资源的主要内容,如果未能解决你的问题,请参考以下文章

使用go webservice在docker中连接到mysql时连接被拒绝

macOS - Dockerize MySQL 服务连接被拒绝,使用时崩溃

docker compose 使用节点 js 和 mysql - 无法与 mysql 连接

docker nodejs容器无法连接mysql容器

Docker-Compose WordPress MySql 无法连接

在 Docker 容器内运行 Spring Boot 应用程序,无法连接 MySQL