使用hikari版本3.4.5设置connectioninitsql时出错

cwtwac6a  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(575)

当我升级我的hikari版本到3.4.5时,我得到了以下错误

The configuration of the pool is sealed once started. Use HikariConfigMXBean for runtime changes.

我在每次测试前运行以下@

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);
  }
}
class TestClass extends DatabaseTestCase {
 @Autowired
  private HikariDataSource dataSource;

@BeforeEach
  void initData() throws SQLException {
    DataSource.ds = dataSource;
    dataSource.getHikariPoolMXBean().softEvictConnections();
    dataSource.setConnectionInitSql("set search_path to " + getTestVitmSchema() + ", public");
  }

这就是我如何设置测试用例的模式

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();
  }

如何设置connection init sql而不出现错误。谢谢您。

fafcakar

fafcakar1#

你不能。你需要创建一个新的 dataSource 做这个。从源代码可以看出原因。

相关问题