nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)

spring 整合 mybatis

报错日志

Exception in thread "main" org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; 
nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect
3 times. Giving up.) ### The error may exist in file [E:\spring\out\production\spring\sqlmap\UserMapper.xml] ### The error may involve com.study.spring.dao.UserMapper.getUserCount ### The error occurred while executing a query ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection;
nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory
(Could not create connection to database server. Attempted reconnect
3 times. Giving up.)

造成这种错误的原因很多,比如数据库连接信息错误

报错前的jdbc.properties 和 application.xml

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
username=root
password=root
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
</bean>

修改成。。。。。。 username   -->  user

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
user=root
password=root
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
</bean>

 

搜集了一下:

1、配置文件写错,比如是某个字母写错了,或者漏掉了 或者 db.properties文本格式不够严谨

2、网络不通导致的.因为是在远程需要vpn. 碰到此问题可以先检查下网络

3、缺少 commons-dbcp jar包

 

posted @ 2021-10-10 08:27  唏嘘-  阅读(1084)  评论(0编辑  收藏  举报