解决Failed to obtain JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException

先上错误截图

解决Failed to obtain JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException_第1张图片

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Public Key Retrieval is not allowed)
### The error may exist in cn/hy/mapper/UserMapper.xml
### The error may involve cn.hy.mapper.UserMapper.selectUserById
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Public Key Retrieval is not allowed)

背景:我使用dbcp配置数据库连接池

按照这个提示,大概就是连接池问题,那么我们看配置文件

  
    <context:property-placeholder location="classpath:db.properties"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    bean>

经过一系列排除,原来是username出现问题
我们用dbcp去配置数据库连接池时,${username}并非db.properties文件中的username。而是映射到了系统环境变量中的username
所以我们在配置db.properties时,通常都是以jdbc或db作为前缀,这样就可以避免重命的问题.
在修改之后,测试一下,没有问题
解决Failed to obtain JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException_第2张图片

你可能感兴趣的:(那些年踩过的坑)