There is a problem with the HikariCP configuration run time

this is the project architecture, which is a simple console test to connect to the mysql database
clipboard.png

this is the configuration file

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 = dataSource.maintainTimeStats

this is the code to test the connection
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());
}

}

Runtime error:
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)

did not find a useful solution on the Internet, hope to get your help, or send a simple, complete and successful demo case

Mar.28,2021
The

error is obvious. If log4j is introduced but there is no log configuration file for Log4j, there will be no problem with the configuration.


after several attempts in recent days, it is solved. It is made with maven and some dependencies have been added. It seems that adding jar packages manually is still easy to make mistakes

.
    <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();
    }
}

}

final result
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

this is the jar package that maven automatically imported for me

Menu