diff --git a/app/api/monolith-api/build.gradle b/app/api/monolith-api/build.gradle index c50f321b..d5fee637 100644 --- a/app/api/monolith-api/build.gradle +++ b/app/api/monolith-api/build.gradle @@ -41,4 +41,5 @@ dependencies { implementation project(':app:consumer:mathrank-contents-consumer-monolith') implementation project(':common:mathrank-cache-caffeine') + implementation project(':common:mathrank-lazy-connection') } diff --git a/common/mathrank-lazy-connection/build.gradle b/common/mathrank-lazy-connection/build.gradle new file mode 100644 index 00000000..1fdfb1ff --- /dev/null +++ b/common/mathrank-lazy-connection/build.gradle @@ -0,0 +1,3 @@ +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' +} diff --git a/common/mathrank-lazy-connection/src/main/java/kr/co/mathrank/common/lazyconnection/LazyConnectionDataSourceProxyConfiguration.java b/common/mathrank-lazy-connection/src/main/java/kr/co/mathrank/common/lazyconnection/LazyConnectionDataSourceProxyConfiguration.java new file mode 100644 index 00000000..39069af4 --- /dev/null +++ b/common/mathrank-lazy-connection/src/main/java/kr/co/mathrank/common/lazyconnection/LazyConnectionDataSourceProxyConfiguration.java @@ -0,0 +1,27 @@ +package kr.co.mathrank.common.lazyconnection; + +import javax.sql.DataSource; + +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; + +import com.zaxxer.hikari.HikariDataSource; + +@Configuration +class LazyConnectionDataSourceProxyConfiguration { + @Bean + HikariDataSource hikariDataSource(final DataSourceProperties dataSourceProperties) { + return dataSourceProperties.initializeDataSourceBuilder() + .type(HikariDataSource.class) + .build(); + } + + @Bean + @Primary + DataSource lazyConnectionDataSource(final HikariDataSource hikariDataSource) { + return new LazyConnectionDataSourceProxy(hikariDataSource); + } +} diff --git a/settings.gradle b/settings.gradle index 75befe68..380173fc 100644 --- a/settings.gradle +++ b/settings.gradle @@ -69,6 +69,7 @@ include( 'domain:mathrank-point-domain', 'common', + 'common:mathrank-lazy-connection', 'common:mathrank-cache', 'common:mathrank-cache-redis', 'common:mathrank-cache-caffeine',