未找到MySql ConnectionPoolDataSource的Cordapp- Hikari连接池类

时间:2019-06-20 13:57:19

标签: mysql corda hikaricp connection-pool

我正在为Corda构建工作流程。我想使用Hikari连接池库连接到MySql数据库。我不是要替换分类帐H2数据库。该数据库用于存储/检索某些信息,账本中不需要这些信息。我无需Hikari就可以连接到MySql。但是,当我使用光时,会出现错误。 java.lang.ClassNotFoundException:com.mysql.cj.jdbc.MysqlConnectionPoolDataSource

我已经测试了Hikari代码,它是一个独立的jar文件。工作正常。这是Corda加载和运行cordapps目录中的jar文件的方式的组合,这导致了问题。由于该类是jar的一部分。似乎有点不对

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

我探索了cordapp罐子。 而且我可以看到cordapp jar中存在必需的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:?]

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

2 个答案:

答案 0 :(得分:0)

在MySQL中,HikariCP使用setJdbcUrl代替setDataSourceClassName

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

众所周知,就网络超时支持而言,MySQL数据源已损坏。改为使用jdbcUrl配置。

答案 1 :(得分:0)

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