Could not get JDBC Connection异常问题

此异常发生在整合SSM过程中.

数据库的properties文件中 用户名的关键词写成了username, 导致报错无法连接

db.properties文件如下:

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///how2java?serverTimezone=GMT
username=root
password=admin

配置文件如下

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"/>
        <property name="jdbcUrl" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
</bean>

异常情况如下:

### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
### The error may exist in file [C:\Users\Tongc\IdeaProjects\ssm_myself\target\classes\mapper\CategoryMapper.xml]
### The error may involve com.cugb.mapper.CategoryMapper.get
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!

通过网上查找得知: 

当出现上面的Exception,而且spring的配置文件如上面两个文件所示时,无法连接数据库的原因在于

<property name="username" value="${username}" /> 这一行因为此时${username}的值并不是jdbc.properties文件中的username值,而是JVM系统环境变量的username。spring容器在管理PropertySource时,不光读取自己写的properties文件,spring也会把JVM system properties和JVM system env properties都读取到容器中,所以请不要使用和JVM properties相同的key。

原文:https://blog.csdn.net/kuohsing/article/details/39452849

posted @ 2019-05-17 17:28  Noino  阅读(20456)  评论(0编辑  收藏  举报