본문으로 건너뛰기

HikariCP 컨넥션풀 설정

스프링 2.x버전부터는 HikariCP가 기본 컨넥션 풀이다

기본 데이터소스에 사용시

spring:  
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3309/springjpadb
hikari:
username: root
password: password
maximum-pool-size: 20

spring.datasource.hikari 관련 property는 다음과 같은 것들이 있다. 

  • spring.datasource.hikari.allow-pool-suspension
  • spring.datasource.hikari.auto-commit
  • spring.datasource.hikari.catalog
  • spring.datasource.hikari.connection-init-sql
  • spring.datasource.hikari.connection-test-query
  • spring.datasource.hikari.connection-timeout
  • spring.datasource.hikari.data-source-class-name
  • spring.datasource.hikari.data-source-j-n-d-i
  • spring.datasource.hikari.data-source-properties
  • spring.datasource.hikari.driver-class-name
  • spring.datasource.hikari.exception-override-class-name
  • spring.datasource.hikari.health-check-properties
  • spring.datasource.hikari.health-check-registry
  • spring.datasource.hikari.idle-timeout
  • spring.datasource.hikari.initialization-fail-timeout
  • spring.datasource.hikari.isolate-internal-queries
  • spring.datasource.hikari.jdbc-url
  • spring.datasource.hikari.keepalive-time
  • spring.datasource.hikari.leak-detection-threshold
  • spring.datasource.hikari.login-timeout
  • spring.datasource.hikari.max-lifetime
  • spring.datasource.hikari.maximum-pool-size
  • spring.datasource.hikari.metric-registry
  • spring.datasource.hikari.metrics-tracker-factory
  • spring.datasource.hikari.minimum-idle
  • spring.datasource.hikari.password
  • spring.datasource.hikari.pool-name
  • spring.datasource.hikari.read-only
  • spring.datasource.hikari.register-mbeans
  • spring.datasource.hikari.scheduled-executor
  • spring.datasource.hikari.schema
  • spring.datasource.hikari.transaction-isolation
  • spring.datasource.hikari.username
  • spring.datasource.hikari.validation-timeout

커스텀 데이터소스 사용시

spring:  
datasource-db1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3309/springjpadb
hikari:
username: root
password: password
maximum-pool-size: 20

위와 같은 방식으로 datasource를 정의하여 java config에서 이를 가져다가 사용하는 경우도 있다.
이런 경우는 java config에서 다음과 같이 설정을 하여 hikari 관련 설정을 바꿔줄 수 있다. 

DatasourceConfig.java

@Configuration
public class DatasourceConfig {

@Value("${spring.datasource-oing.username}")
private String username;

@Value("${spring.datasource-oing.password}")
private String password;

@Value("${spring.datasource-oing.url}")
private String url;

@Bean
@Qualifier("dataSource")
@Primary
public DataSource oingDataSource() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setUsername(username);
hikariConfig.setPassword(password);
hikariConfig.setJdbcUrl(url);
hikariConfig.setMaximumPoolSize(75);
return new HikariDataSource(hikariConfig);
}
..
}

이렇게 했을때는 어떤걸 외부 변수로 뺄지 결정만 한다음 가지고 와서 hikariConfig 설정에 추가를 해주면 된다.