接入HikariCP遇到问题

时间:2024-02-21 20:50:18

老Tomcat项目在接入HikariCP时遇到报错:

Caused by: java.lang.AbstractMethodError: com.mysql.jdbc.Connection.isValid(I)Z
    at com.zaxxer.hikari.pool.PoolBase.checkDriverSupport(PoolBase.java:411) ~[HikariCP-java7-2.4.13.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.setupConnection(PoolBase.java:382) ~[HikariCP-java7-2.4.13.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:346) ~[HikariCP-java7-2.4.13.jar:na]
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:193) ~[HikariCP-java7-2.4.13.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:428) ~[HikariCP-java7-2.4.13.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:499) ~[HikariCP-java7-2.4.13.jar:na]
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:112) ~[HikariCP-java7-2.4.13.jar:na]
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:97) ~[HikariCP-java7-2.4.13.jar:na]

 

解决思路:

先贴出 pom.xml,

<properties>
    <HikariCP>2.4.13</HikariCP>
</properties>
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP-java7</artifactId>
    <version>${HikariCP}</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.0.5</version>
    <scope>runtime</scope>
</dependency>

 配置数据源

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="jdbc:mysql://finance.fd.com:3306/finance_syscnf"/>
            <property name="username" value="CC_dev"/>
            <property name="password" value="hvo3sl234"/>
            <property name="connectionTestQuery" value="select 1"/>
</bean>

 如果不添加 <property name="connectionTestQuery" value="select 1"/>, 那么连接池初始化会报错: