gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:52:43 24 4
gpt4 key购买 nike

我正在为 Corda 构建工作流程。我想使用 Hikari 连接池库来连接 MySql 数据库。我不想替换 Ledger H2 数据库。该数据库用于存储/检索一些信息,这些信息在分类帐中不需要。我无需 Hikari 即可连接到 MySql。但是,当我使用 Hikari 时,出现错误。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 jar 。我可以看到 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);
}
}
}

异常(exception):

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 内部工作

最佳答案

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

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

The MySQL DataSource is known to be broken with respect to network timeout support. Use jdbcUrl configuration instead.

关于mysql - Cordapp-未找到 MySql ConnectionPoolDataSource 的 Hikari 连接池类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56687616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com