public DataSource createHikariDatasource(String url, String username, String password, String driver) {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDataSourceClassName(driver);
hikariConfig.setUsername(username);
hikariConfig.setJdbcUrl(url);
hikariConfig.setPassword(password);
hikariConfig.setConnectionTestQuery("show tables");
DataSource ds = new HikariDataSource(hikariConfig);
return ds;
}
此处的测试用例失败
@Test
public void createHikariCPDatasource() throws Exception {
GreetingConfig greetingConfig = new GreetingConfig();
String driver = "org.hsqldb.jdbc.JDBCDataSource";
String url="jdbc:hsqldb:mem:testdb";
String user = "SA";
String password = "";
logger.debug("get message data source");
DataSource dataSource = greetingConfig.createHikariDatasource(url, user, password,driver);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute(env.getProperty("init"));
jdbcTemplate.execute(env.getProperty("insert"));
}
这里出现错误:(忽略我在上面的测试用例中没有断言状态,因为它无法创建连接才能获得断言。此外,init和insert只是普通的sql初始化并插入一个表/行)
com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: null
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:524)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:118)
at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:71)
at org.cam.go.microservice.GreetingConfig.createHikariDatasource(GreetingConfig.java:233)
at org.cam.go.microservice.GreetingConfigTest.createHikariCPDatasource(GreetingConfigTest.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
可能是什么问题?
答案 0 :(得分:1)
当我没有指定数据源类名时,可以为我工作。
像这样的东西
private static HikariConfig hikariConfig() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:hsqldb:mem:customer");
config.setUsername("sa");
config.setPassword("");
return config;
}