0

我正在为 Corda 构建一个工作流程。我想使用 Hikari 连接池库来连接 MySql 数据库。我不是要替换分类帐 H2 数据库。该数据库用于存储/检索一些分类帐中不需要的信息。我可以在没有 Hikari 的情况下连接到 MySql。但是,当我使用 Hikari 时,出现错误。java.lang.ClassNotFoundException: com.mysql.cj.jdbc.MysqlConnectionPoolDataSource

我已经将 Hikari 代码作为独立的 jar 文件进行了测试。它工作正常。这是导致问题的corda加载和运行jar文件的方式的组合,在cordapps目录中。因为类是 jar 的一部分。这个好像有点不对

我已经添加了 MySql 依赖项,与https://docs.corda.net/cordapp-build-systems.html#setting-your-dependencies中提到的内容一致 我也可以连接到 MySql DB,如果我是不使用光。

我探索了 cordapp jar 。我可以看到必要的 jar 存在于 cordapp jar 中。

cordapp 的 Gradle 依赖项

dependencies {
    testCompile "junit:junit:$junit_version"

    // Corda dependencies.
    cordaCompile "$corda_release_group:corda-core:$corda_release_version"
    cordaRuntime "$corda_release_group:corda:$corda_release_version"

    testCompile "$corda_release_group:corda-node-driver:$corda_release_version"
    runtime "mysql:mysql-connector-java:8.0.11"

    cordaCompile "com.zaxxer:HikariCP:2.5.1"

    // CorDapp dependencies.
    cordapp project(":contracts")
}

示例代码

public class DataSource {
    private static final Logger logger = LoggerFactory.getLogger(DataSource.class);
    private static HikariConfig config = new HikariConfig();
    private static HikariDataSource ds;

    static {
        try {
            logger.info("Connecting with connection pool datasource");
            config.setDataSourceClassName("com.mysql.cj.jdbc.MysqlConnectionPoolDataSource");
            config.addDataSourceProperty("useSSL", "false");
            config.addDataSourceProperty("user", "username");
            config.addDataSourceProperty("password", "password");
            config.addDataSourceProperty("serverName", "localhost");
            config.addDataSourceProperty("useSSL", "false");
            config.addDataSourceProperty("port",Integer.parseInt("3306"));
            config.addDataSourceProperty("cachePrepStmts", "true");
            config.addDataSourceProperty("prepStmtCacheSize", "250");
            config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
            config.addDataSourceProperty("requireSSL", "false");
            config.addDataSourceProperty("serverTimezone", "UTC");
            config.addDataSourceProperty("useServerPrepStmts", "true");
            config.addDataSourceProperty("allowPublicKeyRetrieval", "true");
            config.addDataSourceProperty("databaseName", "database");
            config.setPoolName("Hikari-MySql Pool Name");
            logger.error("-- Create Hikari Datasource with config {} --", config);
            ds = new HikariDataSource(config);
        } catch (Throwable t) {
            logger.error("Error Occurred during Datasource Initializaiton", t);
            throw t;
        }
    }

    private DataSource() {
    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}


public class MySqlConnection {
    static private final Logger logger = LoggerFactory.getLogger(MySqlConnection.class);
    public Connection getMySqlConnection() {
        Connection conn = null;
        try {

            conn = DataSource.getConnection();

            logger.info("------------> Got conne :: " + conn);
        } catch (SQLException e) {
            logger.error("SQLException :: " + e);
        }
        return conn;
    }
}
public class DataSourceTest {
    static private final Logger logger = LoggerFactory.getLogger(DataSourceTest.class);
    public static void main(String[] args) {
        MySqlConnection mySqlConnection = new MySqlConnection();
        Connection conn = null;
        try {

            conn = mySqlConnection.getMySqlConnection();

            logger.info("------------> Got connection :: " + conn);

            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("{some select statement}");
        } catch (SQLException e) {
            logger.error("SQLException :: " + e);
        }
    }
}

例外:

Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.cj.jdbc.MysqlConnectionPoolDataSource
    at com.zaxxer.hikari.util.UtilityElf.createInstance(UtilityElf.java:90) ~[HikariCP-2.5.1.jar:?]
    at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:314) ~[HikariCP-2.5.1.jar:?]
    at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:108) ~[HikariCP-2.5.1.jar:?]
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:99) ~[HikariCP-2.5.1.jar:?]
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:71) ~[HikariCP-2.5.1.jar:?]

如果我从 jar 中的主类运行代码,它就可以工作。但它在cordapp内部不起作用

4

2 回答 2

0

在 MySQL for HikariCP 中使用 setJdbcUrl而不是setDataSourceClassName

config.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");

众所周知,MySQL 数据源在网络超时支持方面已被破坏。请改用 jdbcUrl 配置。

于 2019-06-30T10:58:50.040 回答
0

检查你的依赖树。我认为您与“mysql-connector-java”依赖项有一些冲突,这会导致类加载器混乱。

于 2019-07-02T08:36:27.700 回答