Spring bean中数据库连接报错:java.sql.SQLException: Connections could not be acquired from the underlying dat

利用c3p0数据库连接池配置Spring bean数据源时报错:java.sql.SQLException: Connections could not be acquired from the underlying database! c3p0数据库连接池报错:Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

原因:mysql版本8.0以上,使用jdbc连接时url需加上时区等信息,驱动driverClass需改成com.mysql.cj.jdbc.Driver

解决方法:

1)maven中配置高版本mysql-connector-javac3p0包:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
    </dependency>
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.2</version>
    </dependency>

2.1)由于bean&字符的限制,在resources中添加配置文件c3p0.properties配置url

url=jdbc:mysql://localhost:3306/eesy?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT

2.1.1)bean中引用外部配置文件c3p0.properties

<!--配置数据源-->
    <context:property-placeholder location="classpath:c3p0.properties"/>
    <bean id="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--连接数据库的必备信息-->
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<!--        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property>-->
        <property name="jdbcUrl" value="${url}"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

2.2.1)或者在xml使用&amp;代替&

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值