1、flink使用hikariCP数据库连接池
java
private static volatile HikariDataSource dataSource;
private static final Object SOURCE_LOCK = new Object();
private DataSource() {
}
private static HikariDataSource getDataSource(StartupParams startupParams) {
if (dataSource== null || dataSource.isClosed()) {
synchronized (SOURCE_LOCK) {
if (dataSource== null || dataSource.isClosed()) {
HikariConfig config = new HikariConfig();
try {
config.setJdbcUrl("XXX");
config.setUsername("XXX");
config.setPassword("XXX");
config.setMaximumPoolSize(20);
config.setMinimumIdle(10);
config.setConnectionTimeout(30000);
} catch (Exception e) {
log.error("初始化HikariCP连接池配置时发生异常,{}", ExceptionUtils.getStackTrace(e));
}
dataSource= new HikariDataSource(config);
}
}
}
return dataSource;
}
在flink中使用hikariCP当做连接池
2、hikariCP的连接泄露
①在flink的启动模式中,如果是job和task分开部署时,最好不在job中使用连接池,否则会在jobmanager中的持有连接
②所有用到连接的地方都要关闭,try-catch-flinally关闭连接