Java 使用Hikari版本3.4.5设置ConnectionInitSql时出错

Java 使用Hikari版本3.4.5设置ConnectionInitSql时出错,java,database,maven,junit,hikaricp,Java,Database,Maven,Junit,Hikaricp,当我将Hikari版本升级到3.4.5时,出现以下错误 The configuration of the pool is sealed once started. Use HikariConfigMXBean for runtime changes. 我正在运行以下@beforeach测试 dataSource.getHikariPoolMXBean().softEvictConnections(); dataSource.setConnectionInitSql("set se

当我将Hikari版本升级到3.4.5时,出现以下错误

The configuration of the pool is sealed once started. Use HikariConfigMXBean for runtime changes.
我正在运行以下@beforeach测试

 dataSource.getHikariPoolMXBean().softEvictConnections();
 dataSource.setConnectionInitSql("set search_path to " + getTestSchema() + ", public");
这就是我加载数据源的方式

public class DataSource {
  private static HikariConfig config = new HikariConfig();
  public static HikariDataSource ds;

  static {
    config.setJdbcUrl(
        System.getProperty("jdbc.url", "jdbc:postgresql://localhost:123/abc"));
    config.setUsername(System.getProperty("username", "xyz"));
    config.setPassword(System.getProperty("password", "123"));
    ds = new HikariDataSource(config);
  }

  private DataSource() {}

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

  public static void evictConnection(Connection connection) {
    ds.evictConnection(connection);
  }
}
这就是我为测试用例设置模式的方式

public abstract class DatabaseTestCase {

  private String testSchema;

  @BeforeEach
  final void setupSchema() {
    int classHash = Math.abs(getClass().getSimpleName().hashCode());
    testSchema = String.format("test_%s", classHash);
    Map<String, String> placeholders = new HashMap<>();
    placeholders.put("schema", testSchema);
    placeholders.put("name", "xyz");
    placeholders.put("password", "123");
    placeholders.putAll(getPlaceholderReplacement());
    Flyway flyway =
        Flyway.configure()
            .dataSource(DataSource.ds)
            .schemas(testSchema)
            .placeholders(placeholders)
            .load();
    flyway.migrate();
  }
公共抽象类数据库testcase{
私有字符串测试模式;
@之前
最终void setupSchema(){
int classHash=Math.abs(getClass().getSimpleName().hashCode());
testSchema=String.format(“test\u%s”,classHash);
映射占位符=新HashMap();
placeholders.put(“schema”,testSchema);
占位符。放置(“名称”、“xyz”);
占位符。放置(“密码”、“123”);
putAll(getPlaceholderReplacement());
跑道=
configure()
.dataSource(dataSource.ds)
.schema(testSchema)
.占位符(占位符)
.load();
flyway.migrate();
}

如何设置Connection Init Sql而不出现错误。谢谢。

您不能。您需要创建一个新的
数据源来执行此操作。您可以通过查看。

了解为什么您需要在测试中以不同的方式加载数据源,如何加载数据源?使用属性或代码?您可以共享示例吗?将问题更新为包含了解数据源的详细信息。谢谢。我怎样才能为测试创建一个数据源。你能指导我吗?谢谢。@Laird Nelson
public abstract class DatabaseTestCase {

  private String testSchema;

  @BeforeEach
  final void setupSchema() {
    int classHash = Math.abs(getClass().getSimpleName().hashCode());
    testSchema = String.format("test_%s", classHash);
    Map<String, String> placeholders = new HashMap<>();
    placeholders.put("schema", testSchema);
    placeholders.put("name", "xyz");
    placeholders.put("password", "123");
    placeholders.putAll(getPlaceholderReplacement());
    Flyway flyway =
        Flyway.configure()
            .dataSource(DataSource.ds)
            .schemas(testSchema)
            .placeholders(placeholders)
            .load();
    flyway.migrate();
  }