Connections could not be acquired from the underlying database!

本文详细解析了一个关于Spring框架中使用JDBC进行数据库操作时出现的无法获取连接异常问题。异常的根本原因是数据库数据源配置错误,特别是连接参数中存在非法字符如空格导致的数据源初始化失败。文章通过具体的异常堆栈跟踪,指导读者如何定位和解决此类问题,强调了检查数据源配置的重要性。

报错信息

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!

	at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:615)
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:866)
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:927)
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:937)
	at com.ujiuye.dao.AccountDaoImpl.decrreateMoney(AccountDaoImpl.java:19)
	at com.ujiuye.service.AccountServiceImpl.trader(AccountServiceImpl.java:31)
	at com.ujiuye.test.TranTest.test1(TranTest.java:20)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
	at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
	... 29 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)
	... 32 more


Process finished with exit code -1

解决方法

基本都是由于数据库数据源的配置问题 我的是在*.properties* 文件里的
在这里插入图片描述
这个位置有一个空格,所以出现这个问题,就要好好找找自己的数据源问题了
尤其是几个连接参数

### 错误原因分析 `Connections could not be acquired from the underlying database` 错误通常表明应用程序无法从数据库连接池中获取数据库连接。根据提供的信息,以下是一些可能的原因: 1. **驱动版本不兼容**:如果使用的 `mysql-connector-java` 驱动版本与数据库版本不兼容,可能会导致连接失败。例如,使用 MySQL 8.0 数据库时,若使用的是 5.1.6 版本的驱动,可能会出现兼容性问题。换用 8.0.11 或更高版本的驱动后,问题得以解决 [^1]。 2. **驱动类名错误**:在 MySQL 8.0 及以上版本中,正确的驱动类名为 `com.mysql.cj.jdbc.Driver`。如果配置错误,可能导致连接失败 [^2]。 3. **数据库连接地址错误**:数据库连接 URL 配置错误也可能导致连接失败。例如,URL 中可能缺少必要的参数,如 `serverTimezone` 属性 [^2]。 4. **数据库服务未启动或权限问题**:如果数据库服务未启动,或者用户没有访问数据库的权限,也会导致连接失败 [^3]。 5. **依赖引入问题**:如果项目中引入的 MySQL 依赖无效或版本不匹配,也可能导致连接失败。例如,如果数据库是 MySQL 8.0.21,则应引入对应的 `mysql-connector-8.0.16.jar` 包 [^3]。 ### 解决方案 1. **更新驱动版本**:确保使用的 `mysql-connector-java` 驱动版本与数据库版本兼容。例如,对于 MySQL 8.0 数据库,使用 8.0.11 或更高版本的驱动 [^1]。 2. **检查驱动类名**:确保在配置文件中使用了正确的驱动类名 `com.mysql.cj.jdbc.Driver` [^2]。 3. **验证数据库连接地址**:检查数据库连接 URL 是否正确,特别是是否包含了必要的参数,如 `serverTimezone` [^2]。 4. **检查数据库服务状态和权限**:确保数据库服务正在运行,并且用户具有访问数据库的权限 [^3]。 5. **确认依赖引入正确**:确保项目中引入的 MySQL 依赖有效且版本正确。例如,如果数据库是 MySQL 8.0.21,则应引入对应的 `mysql-connector-java` 包 [^3]。 ### 示例代码 ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值