HikariCP配置运行时出了问题

这是项目架构,就是一个简单的控制台测试连接mysql数据库
clipboard.png

这是配置文件

jdbcUrl = jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
user = root
password = 123456
dataSource.cachePrepStmts = true
dataSource.prepStmtCacheSize = 250
dataSource.prepStmtCacheSqlLimit = 2048
dataSource.useServerPrepStmts = true
dataSource.useLocalSessionState = true
dataSource.useLocalTransactionState=true
dataSource.rewriteBatchedStatements = true
dataSource.cacheResultSetMetadata = true
dataSource.cacheServerConfiguration = true
dataSource.elideSetAutoCommits = true
dataSource.maintainTimeStats = false

这是测试连接的代码
import java.sql.Connection;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public class HikariPoolManager {

static Logger logger = LogManager.getLogger();
static HikariConfig config = new HikariConfig("/hurik.properties");
static HikariDataSource ds = new HikariDataSource(config);

/**
 * 取得数据库连接
 * 
 * @return
 * @throws Exception
 */
public static Connection getConnection() throws Exception {

    Connection connection = null;
    try {
        connection = ds.getConnection();
    } catch (Exception e) {
        logger.error("取得数据库连接时发生异常!" + e);
        throw e;
    }
    return connection;
}

/**
 * 释放数据库连接
 * 
 * @param connection
 * @throws Exception
 */
public static void freeConnection(Connection connection) throws Exception {
    if (connection != null) {
        try {
            connection.close();
        } catch (Exception e) {
            logger.error("释放数据库连接时发生异常!" + e.getMessage());
        }
    }
}

public static void main(String[] args) throws Exception {
    System.out.println(ds.getConnection());
}

}

运行时出的错误:
ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/lo... for instructions on how to configure Log4j 2
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.ht... for further details.
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Property user does not exist on target class com.zaxxer.hikari.HikariConfig

at com.zaxxer.hikari.util.PropertyElf.setProperty(PropertyElf.java:131)
at com.zaxxer.hikari.util.PropertyElf.lambda$setTargetFromProperties$0(PropertyElf.java:57)
at java.util.Hashtable.forEach(Unknown Source)
at com.zaxxer.hikari.util.PropertyElf.setTargetFromProperties(PropertyElf.java:52)
at com.zaxxer.hikari.HikariConfig.loadProperties(HikariConfig.java:1067)
at com.zaxxer.hikari.HikariConfig.<init>(HikariConfig.java:148)
at HikariPoolManager.<clinit>(HikariPoolManager.java:11)

没有从网上找到有用的解决方案,希望能得到各位的帮助,或者能发一份简单完整又测试成功的demo案例也行

阅读 8.7k
2 个回答

错误已经很明显了,引入log4j却没有配置Log4j的日志配置文件,配置上就没有问题了。

最近几天又做了几次尝试后还是解决了,是使用的maven做的,添加了一些依赖,看样子手动添加jar包还是容易出错
<!--jdbc依赖 -->

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
    </dependency>
    <!-- log4j依赖 -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
    <!-- json格式依賴 -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.17</version>
    </dependency>
    <!--Hikari依赖 -->
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>3.1.0</version>
    </dependency>

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public class HikarTest {

private static Connection conn = null;
private static Statement statement = null;
private static ResultSet rs = null;
private static HikariDataSource ds;
static {

    initDBSource();

}

public static void initDBSource() {
    // TODO Auto-generated method stub
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8");
    hikariConfig.setUsername("root");
    hikariConfig.setPassword("123456");
    hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
    hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
    hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

    ds = new HikariDataSource(hikariConfig);

}

/**
 * 取得数据库连接
 * 
 * @return
 * @throws Exception
 */
public static Connection getConnection() throws Exception {

    Connection conn = null;
    try {
        conn = ds.getConnection();
    } catch (Exception e) {
        throw e;
    }
    return conn;
}

/**
 * 释放数据库连接
 * 
 * @param connection
 * @throws Exception
 */
public static void freeConnection(Connection conn) throws Exception {
    if (conn != null) {
        try {
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        }
    }
}

public static void main(String[] args) {

    try {
        conn = ds.getConnection();
        statement = conn.createStatement();
        rs = statement.executeQuery("select * from dept");

        while (rs.next()) {
            System.out.println(rs.getString("dname"));
            System.out.println(rs.getString("loc"));
            System.out.println(rs.getInt("deptno"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}

最终结果
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.ht... for further details.
ACCOUNTING
NEW YORK
10
RESEARCH
DALLAS
20
SALES
CHICAGO
30
OPERATIONS
BOSTON
40

这是maven自动为我导入的jar包

图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题