HikariCP Connection 설정하기
HikariCP는 가장 대중적으로 많이 사용하는 고성능 JDBC Connection Pool입니다. 이것을 사용하기 위해서는 src/main/resources/application.yml
파일에 다음과 같이 설정값들을 사용할 수 있습니다.
spring: application: name: HelloWorld ################### ## JDBC ################### datasource: hikari: jdbc-url: jdbc:postgresql://localhost:5432/test username: postgres password: postgres driver-class-name: org.postgresql.Driver connection-test-query: SELECT 1 maximum-pool-size: 10 minimum-idle: 3 sql-script-encoding: UTF-8 continue-on-error: true initialization-mode: always
HikariCP 설정에서 JDBC를 설정하지 않고 공용 속성으로 설정하고 필요한 설정만 HikariCP 설정으로 지정할 수도 있습니다.
spring: application: name: HelloWorld ################# ## DataSource ################# datasource: url: jdbc:postgresql://localhost:5432/test driver-class-name: org.postgresql.Driver username: postgres password: postgres sql-script-encoding: UTF-8 continue-on-error: true initialization-mode: always hikari: connection-test-query: SELECT 1 maximum-pool-size: 10 max-lifetime: 1800000 minimum-idle: 10 idle-timeout: 600000
org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration
설정에 다음과 같이 HikariCP를 자동 초기화 하는 코드가 포함되어 있습니다.
import java.sql.SQLException; import javax.sql.DataSource; import com.zaxxer.hikari.HikariDataSource; import oracle.jdbc.OracleConnection; import oracle.ucp.jdbc.PoolDataSourceImpl; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DatabaseDriver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.StringUtils; abstract class DataSourceConfiguration { @SuppressWarnings("unchecked") protected static <T> T createDataSource(DataSourceProperties properties, Class<? extends DataSource> type) { return (T) properties.initializeDataSourceBuilder().type(type).build(); } /** * Hikari DataSource configuration. */ @Configuration(proxyBeanMethods = false) @ConditionalOnClass(HikariDataSource.class) @ConditionalOnMissingBean(DataSource.class) @ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource", matchIfMissing = true) static class Hikari { @Bean @ConfigurationProperties(prefix = "spring.datasource.hikari") HikariDataSource dataSource(DataSourceProperties properties) { HikariDataSource dataSource = createDataSource(properties, HikariDataSource.class); if (StringUtils.hasText(properties.getName())) { dataSource.setPoolName(properties.getName()); } return dataSource; } } /** * Generic DataSource configuration. */ @Configuration(proxyBeanMethods = false) @ConditionalOnMissingBean(DataSource.class) @ConditionalOnProperty(name = "spring.datasource.type") static class Generic { @Bean DataSource dataSource(DataSourceProperties properties) { return properties.initializeDataSourceBuilder().build(); } } }
Spring Boot JDBC Starter 추가하기
데이터베이스를 사용하려면 다음과 같이 Spring Boot JDBC Starter를 Maven POM인 pom.xml
파일에 추가하도록 합니다.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
HikariCP 추가하기
HikariCP를 사용하기 위해서는 다음과 같이 JDBC Driver와 HikariCP를 mvnrepository.com에서 찾아서 Maven POM인 pom.xml
파일에 추가하도록 합니다.
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>${hikaricp.version}</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>${postgres.jdbc.version}</version> </dependency>
DataSource 정의
이제 다음과 같이 수동으로 DataSource와 JDBC Template을 정의합니다. 단, 다음의 DataSource 설정은 꼭 필요한 경우만 하도록 하고 별도로 설정하지 않더라도 YAML 파일에 설정하고 관련 Dependency만 추가하면 spring-boot-starter-data-jpa 만 추가하면 자동으로 데이터소스가 생성됩니다.
import com.zaxxer.hikari.HikariDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; @Configuration public class DataSourceConfiguration { @Bean(name = "dataSource") @Primary @ConfigurationProperties("spring.datasource.hikari") public DataSource dataSource() { return DataSourceBuilder.create().type(HikariDataSource.class).build(); } @Bean JdbcTemplate jdbcTemplate() { return new JdbcTemplate(dataSource()); } }