티스토리 뷰

기존에는 jndi를 사용해서 tomcat에서 datasource 관련 설정을 할때는 tomcat 가이드에 있는대로 설정을 하면 되었다. 하지만 이제는 springboot 사용이 보편화됨에 따라 embedded tomcat을 사용하므로 application 단에서 datasource 관련설정을 모두 해야 한다. application 단에서 datasource 관련설정을 어떻게 하는지, 그중에서도 대표적인 Connection Pool 설정 방법에 대해서 알아보겠다. 

참고로 필자의 환경은 springboot 2.5.x 이다. 


1. spring default datasource 사용 시 (application.yml)

spring:
  datasource:
    driver-class-name: oracle.jdbc.OracleDriver
    url: jdbc:oracle:thin:@xx.xx.xx.xx:1521:OINGDB
    hikari:
      username: oing
      password: daddy
      maximum-pool-size: 20

spring.datasource 라는 기본적으로 datasource bean으로 사용할 수 있는 설정에 대해서는 위와 같이 application.yml 파일에서 설정을 할 수 있다. springboot 2.x 에서는 hikari가 기본 CP라서 spring.datasource.tomcat.* 에 대한 설정이 가능할 줄 알았는데 안먹는다. 그래서 위와 같이 spring.datasource.hikari.* 으로 Connection Pool 을 비롯한 설정을 해줘야 한다. 

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

2. custom datasource 사용시 (application.yml)

spring:
  datasource-oing:
    driver-class-name: oracle.jdbc.OracleDriver
    url: jdbc:oracle:thin:@xx.xx.xx.xx:1521:OINGDB
    username: oing
    password: daddy

위와 같은 방식으로 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 설정에 추가를 해주면 된다. 

 

3. 테스트

이렇게 설정을 하고 Connection Pool 관련 설정이 잘 적용되었는지 보려면 com.* 관련 로그를 debug level로 설정해주고 재기동을 해보면 된다. 

[com.zaxxer...hikari.HikariConfig.. - HikariPool-1 - configuration:
[com.zaxxer...hikari.HikariConfig.. - allowPoolSuspension................................false
[com.zaxxer...hikari.HikariConfig.. - autoCommit................................true
[com.zaxxer...hikari.HikariConfig.. - catalog................................none
[com.zaxxer...hikari.HikariConfig.. - connectionInitSql................................none
[com.zaxxer...hikari.HikariConfig.. - connectionTestQuery................................none
[com.zaxxer...hikari.HikariConfig.. - connectionTimeout................................30000
[com.zaxxer...hikari.HikariConfig.. - dataSource................................none
[com.zaxxer...hikari.HikariConfig.. - dataSourceClassName................................none
[com.zaxxer...hikari.HikariConfig.. - dataSourceJNDI................................none
[com.zaxxer...hikari.HikariConfig.. - dataSourceProperties................................{password=<masked>}
[com.zaxxer...hikari.HikariConfig.. - driverClassName................................none
[com.zaxxer...hikari.HikariConfig.. - exceptionOverrideClassName................................none
[com.zaxxer...hikari.HikariConfig.. - healthCheckProperties................................{}
[com.zaxxer...hikari.HikariConfig.. - healthCheckRegistry................................none
[com.zaxxer...hikari.HikariConfig.. - idleTimeout................................600000
[com.zaxxer...hikari.HikariConfig.. - initializationFailTimeout................................1
[com.zaxxer...hikari.HikariConfig.. - isolateInternalQueries................................false
[com.zaxxer...hikari.HikariConfig.. - jdbcUrl................................jdbc:oracle:thin:@xx.xx.xx.xx:1521:OINGDB
[com.zaxxer...hikari.HikariConfig.. - keepaliveTime................................0
[com.zaxxer...hikari.HikariConfig.. - leakDetectionThreshold................................0
[com.zaxxer...hikari.HikariConfig.. - maxLifetime................................1800000
[com.zaxxer...hikari.HikariConfig.. - maximumPoolSize................................75
[com.zaxxer...hikari.HikariConfig.. - metricRegistry................................none
[com.zaxxer...hikari.HikariConfig.. - metricsTrackerFactory................................none
[com.zaxxer...hikari.HikariConfig.. - minimumIdle................................75
[com.zaxxer...hikari.HikariConfig.. - password................................<masked>
[com.zaxxer...hikari.HikariConfig.. - poolName................................"HikariPool-1"
[com.zaxxer...hikari.HikariConfig.. - readOnly................................false
[com.zaxxer...hikari.HikariConfig.. - registerMbeans................................false
[com.zaxxer...hikari.HikariConfig.. - scheduledExecutor................................none
[com.zaxxer...hikari.HikariConfig.. - schema................................none
[com.zaxxer...hikari.HikariConfig.. - threadFactory................................internal
[com.zaxxer...hikari.HikariConfig.. - transactionIsolation................................default
[com.zaxxer...hikari.HikariConfig.. - username................................"oing"
[com.zaxxer...hikari.HikariConfig.. - validationTimeout................................5000

23 line을 보면 설정한대로 Connection Pool Size가 정상적으로 변경된것을 확인할 수 있다. 

 

끝!

댓글
최근에 올라온 글
최근에 달린 댓글
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30