java.sql.SQLException:无法从底层数据库获取连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24835592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: Connections could not be acquired from the underlying database
提问by user121196
I'm using Hibernate, MySQL connection is valid but I get the following. How do I see more details on connection string that it's connecting to?
我正在使用 Hibernate,MySQL 连接有效,但我得到以下信息。如何查看有关它所连接的连接字符串的更多详细信息?
ERROR SchemaUpdate - could not get database metadata
java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:168)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:356)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
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:1319)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
... 10 more
ERROR SchemaUpdate - could not complete schema update
java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:168)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:356)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
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:1319)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
... 10 more
ERROR JDBCExceptionReporter - Connections could not be acquired from the underlying database!
org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
at com.sun.proxy.$Proxy0.beginTransaction(Unknown Source)
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 15 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:1319)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
... 18 more
here's my hibernate xml:
这是我的休眠 xml:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">xxxxxx</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_statements">200</property>
<property name="hibernate.c3p0.min_size">20</property>
<property name="hibernate.c3p0.max_size">200</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">200</property>
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
回答by Hemant Nagpal
I will recommend two things to get this problem resolved : First, try mentioning your database name in capital letters. Though db is case insensitive still , sometimes it runs fine with caps.
我将推荐两件事来解决这个问题:首先,尝试用大写字母提及您的数据库名称。尽管 db 仍然不区分大小写,但有时它可以用大写字母运行。
Secondly , this problem may arise because of some extra stuff you might have appended with your session factory value. For example (i will take code i used and faced this problem and the got rid of it) : Part of my xml goes like this :
其次,这个问题的出现可能是因为您可能在会话工厂值中附加了一些额外的东西。例如(我将使用我使用过的代码并遇到这个问题并摆脱它):我的 xml 的一部分是这样的:
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://10.184.56.183:3306/******?relaxAutoCommit=true&autoReconnect=true&useUnicode=true&characterEncoding=utf8</property>
<property name="connection.username">******</property>
<property name="connection.password">******</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.connection.pool_size">5</property>
<property name="hibernate.connection.shutdown">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.max_size">5</property>
<property name="hibernate.c3p0.timeout">108000</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">1000</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.c3p0.acquire_increment">5</property>
So keep trimming extra values like clean your database connection url and other stuff. I faced problem while generating code from database table as well. So i created a different config xml and made that as simple as possible and thats it. It went very fine .
因此,请继续修剪额外的值,例如清理数据库连接 url 和其他内容。我在从数据库表生成代码时也遇到了问题。所以我创建了一个不同的配置 xml 并使其尽可能简单,仅此而已。一切顺利。
回答by masterdany88
In my case it was problem that c3p0-0.9.2.1.jar file was not copied by maven into src/main/webapp/web-inf/libs
就我而言,问题是 maven 没有将 c3p0-0.9.2.1.jar 文件复制到 src/main/webapp/web-inf/libs
回答by dazito
I had this problem and the issue was our office IP address being blocked by MySQL server due to many connect attempts using bad credentials.
我遇到了这个问题,问题是我们的办公室 IP 地址被 MySQL 服务器阻止,因为多次尝试使用错误凭据进行连接。
Make sure you can connect to the server using the same method/network as your application. If you get this or something like this when attempting to connect using some MySQL client (like Workbench)
确保您可以使用与您的应用程序相同的方法/网络连接到服务器。如果您在尝试使用某些 MySQL 客户端(例如 Workbench)进行连接时遇到此问题或类似问题
Host 'XXX-XXX-XXX-XXX.static.XXXXXX.XXX' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
Your IP is blocked and that is the issue.
您的 IP 被阻止,这就是问题所在。
回答by Maurice Elagu
I solved this challange by following Confluence Supportarticle.
我通过遵循Confluence 支持文章解决了这个挑战。
The Causes and Resolutions as stated by the above blog post for the issue of
cannot establish a connection to its database.
are
上述博客文章所述的原因和解决方案
cannot establish a connection to its database.
是
The database configuration details you entered are incorrect or have changed.For example, the database name or database username/password for your Confluence database. Confirm that the database username, password, and connection URL in /confluence.cfg.xml are incorrect, and if not update them accordingly.
The URL for the database is incorrect.For example, if the database connection is jdbc:mysql://yma/confluence?autoReconnect=true, does the host 'yma' exist and is the database server installed on 'yma'? Check /confluence.cfg.xml to find this entry. The database server is not running. If your database server is down, restart it and check the logs to see why it had failed or stopped.
The network is down (or there is a firewall in between Confluence and its database) and the connection to the databasecannot be established. If your network is down, or a new firewall is installed, consult with your System Administrator for further troubleshooting.
Your database password has expired.If your password has expired (and you will only discover this if you try to connect to your database via your database client or an external database tool such as DbVisualizer), update your password. You may want to deactivate expiring passwords for your Confluence database user to prevent the problem from happening again. Be sure to consult with your DBA about this.
Postgres specific issue:the Postgres encryption method has been altered or changed when migrating or upgrading Confluence, the environment, etc. Check the pg_hba.conf file. If the method is set to ident change it to md5.
MySQL specific issue:there are too many connection requests that triggered MySQL server to block Confluence from making any requests. See this MySQL article for resolution.
您输入的数据库配置详细信息不正确或已更改。例如,您的 Confluence 数据库的数据库名称或数据库用户名/密码。确认 /confluence.cfg.xml 中的数据库用户名、密码和连接 URL 不正确,如果不正确则相应更新。
数据库的 URL 不正确。例如,如果数据库连接是jdbc:mysql://yma/confluence?autoReconnect=true,那么主机'yma'是否存在,数据库服务器是否安装在'yma'上?检查 /confluence.cfg.xml 以找到此条目。数据库服务器没有运行。如果您的数据库服务器已关闭,请重新启动它并检查日志以了解它失败或停止的原因。
网络已关闭(或 Confluence 与其数据库之间存在防火墙)并且无法建立与数据库的连接。如果您的网络出现故障,或者安装了新的防火墙,请咨询您的系统管理员以进行进一步的故障排除。
您的数据库密码已过期。如果您的密码已过期(并且只有在您尝试通过数据库客户端或 DbVisualizer 等外部数据库工具连接到数据库时才会发现这种情况),请更新您的密码。您可能希望为您的 Confluence 数据库用户停用过期密码,以防止问题再次发生。请务必就此咨询您的 DBA。
Postgres 特定问题:在迁移或升级 Confluence、环境等时,Postgres 加密方法已被更改或更改。检查 pg_hba.conf 文件。如果方法设置为 ident 将其更改为 md5。
MySQL 特定问题:有太多连接请求触发 MySQL 服务器阻止 Confluence 发出任何请求。请参阅此 MySQL 文章以获取解决方案。