使用Hikari池进行主从数据库配置

时间:2019-10-16 09:59:18

标签: hikaricp master-slave

我有两个带有@Primary批注的DataSource Bean。 为每个数据源创建单独的Hikari池。

我正在尝试将HikariDataSource从池1(如果连接不可用)更改为池2。

@Primary
@Bean(destroyMethod = "close", name = "dataSource")
public CustomHikariDataSource dataSource() throws SQLException {
    try {
        primaryDataSource = mainDataSource();
    } catch (Exception e) {
        primaryDataSource = secondaryDataSource();
    }
    HikariConfig config = new HikariConfig();
    config.setDataSource(primaryDataSource);
    config.setPoolName("POOL_PRIMARY");
    config.setAllowPoolSuspension(true);
    config.setIdleTimeout(10000);
    config.setMaxLifetime(30000);
    return new CustomHikariDataSource(config);
}

@Bean(destroyMethod = "close", name = "failoverDataSource")
public CustomHikariDataSource failoverDataSource() throws SQLException {
    secondaryDataSource = secondaryDataSource();
    HikariConfig config = new HikariConfig();
    config.setDataSource(secondaryDataSource);
    config.setPoolName("POOL_SECONDARY");
    config.setAllowPoolSuspension(true);
    return new CustomHikariDataSource(config);
}

private DataSource mainDataSource() {
    return dataSourceProperties().initializeDataSourceBuilder().build();

}

private DataSource secondaryDataSource() {
    return failoverDataSourceProperties().initializeDataSourceBuilder().build();

}

实际问题在哪里?

1 个答案:

答案 0 :(得分:0)

最后我可以通过覆盖HikariDataSource.class的getConnection()方法来实现它

@Override
public Connection getConnection() throws SQLException {

    if (isClosed()) {
        throw new SQLException("HikariDataSource " + this + " has been closed.");
    }

    if (fastPathPool != null && (fastPathPool.poolState == 0 || fastPathPool.poolState == 1)) {
        try {
            fastPathPool.resumePool();
            con = fastPathPool.getConnection();
        } catch (Exception e) {

        }
        if (con.isClosed()) {
            config = pool.config;
            fastPathPool.suspendPool();
        } else
            return con;
    }

    config.setDataSource(dataSource);
    config.setAllowPoolSuspension(true);
    config.setMinimumIdle(minIdle);
    pool = new HikariPool(config);
    HikariPool result = pool;
    if (result == null) {
        synchronized (this) {
            result = pool;
            if (result == null) {
                validate();
                System.out.println("{} - Starting..." + getPoolName());
                try {
                    pool = result = new HikariPool(this);
                    this.seal();
                } catch (PoolInitializationException pie) {
                    if (pie.getCause() instanceof SQLException) {
                        throw (SQLException) pie.getCause();
                    } else {
                        throw pie;
                    }
                }
                System.out.println("{} - Start completed." + getPoolName());
            }
        }
    }

    return result.getConnection();

}

要完成课程,请随时给我ping通。

快乐编码! :)