1. 程式人生 > 實用技巧 >c3p0 配置連線MySQL異常:java.sql.SQLException: Connections could not be acquired from the underlying database!

c3p0 配置連線MySQL異常:java.sql.SQLException: Connections could not be acquired from the underlying database!

在Idea上配置c3p0連線MySQL時報如下錯誤:

警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@4e745ea8 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (2). Last acquisition attempt exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

java.sql.SQLException: Connections could not be acquired from the underlying database!

Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

使用的配置為:

spring的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"
?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--配置連線池屬性--> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!--c3p0連線池的私有屬性--> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!--關閉連線後不自動commit--> <property name="autoCommitOnClose" value="false"/> <!--獲取連線超時時間--> <property name="checkoutTimeout" value="10000"/> <!--設定獲取連線失敗重試次數--> <property name="acquireRetryAttempts" value="2"/> </bean> </beans>

jdbc.properties檔案:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xiaoyanger?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=1234

pom檔案中關於mysql的依賴:

<!--資料庫-->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.37</version>
</dependency>
<dependency>
  <groupId>c3p0</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.1.2</version>
</dependency>

Main.java程式碼:

import javax.sql.DataSource;
import java.sql.SQLException;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ioc=new ClassPathXmlApplicationContext("META-INF/applicationContext.xml");
        DataSource bean= (DataSource) ioc.getBean("dataSource");
        try {
            System.out.println(bean.getConnection());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("容器啟動完成。。。");
    }
}

排查:

1,驅動配置有誤:driver=com.mysql.jdbc.Driver
2,資料庫連線地址有誤:url=jdbc:mysql://localhost:3306/test?3useUnicode=true&characterEncoding=utf8
3,密碼或帳號有誤:username=rootpassword=root

(上面三條一般都寫在配置檔案中,如果是因為修改了配置檔案後導致該錯誤,建議重寫一遍配置檔案,因為有時候開發工具就是很蛋疼,表面沒有錯誤,程式執行卻提示報錯)

4,資料庫未啟動或無權訪問

5,專案未引入對應的驅動jar包mysql-connector-java-5.1.6-bin.jar

6,mysql root沒有遠端訪問的許可權,需要增加許可權,增加許可權的步驟如下:
進入mysql資料庫:
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
flush privileges;

7.在普通java專案中使用 mybatis+C3p0+spring時也遇見上述問題。
問題原因是:在xml配置檔案中顯示聲明瞭自動裝載 <beans default-autowire="byName">幾經折騰,把自動裝載設定去掉,就沒有問題了,應該使用預設的 byType。

經過以上方式排查均未解決問題,還是報錯!!

解決該問題辦法:

修改pom檔案中mysql驅動的版本,改為8.0.11:
<!--資料庫-->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.11</version>
</dependency>
<dependency>
  <groupId>c3p0</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.1.2</version>
</dependency>
將jdbc.properties的jdbc.driver=com.mysql.jdbc.Driver改為jdbc.driver=com.mysql.cj.jdbc.Driver

再次執行發現報如下錯誤

  java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver

  很明顯是時區的問題,證明已經連線上了MySQL

解決時區問題

1、在連線url後加serverTimezone=UTC(UTC是世界均衡時間),即修改jdbc.properties的jdbc.url為:
jdbc.url=jdbc:mysql://localhost:3306/xiaoyanger?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC

2、進入mysql命令列模式執行語句:
set global time_zone='+8:00'

我採用的是方法1,再次執行,無報錯,問題解決。