当前位置:   article > 正文

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

springmvc sqlserver connections could not be acquired from the underlying da

利用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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

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

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/627469
推荐阅读
相关标签
  

闽ICP备14008679号