From 973703d53c717daae022c8e6080ef05b7e29dd44 Mon Sep 17 00:00:00 2001 From: HoeSeong123 Date: Thu, 26 Sep 2024 20:03:50 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20rds=20reader=20writer=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codezap/global/rds/DataSourceConfig.java | 63 +++++++++++++++++++ .../codezap/global/rds/DataSourceRouter.java | 17 +++++ .../resources/logger/logback-spring-prod.xml | 3 +- 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/codezap/global/rds/DataSourceConfig.java create mode 100644 backend/src/main/java/codezap/global/rds/DataSourceRouter.java diff --git a/backend/src/main/java/codezap/global/rds/DataSourceConfig.java b/backend/src/main/java/codezap/global/rds/DataSourceConfig.java new file mode 100644 index 000000000..af642d0fb --- /dev/null +++ b/backend/src/main/java/codezap/global/rds/DataSourceConfig.java @@ -0,0 +1,63 @@ +package codezap.global.rds; + +import java.util.HashMap; +import java.util.Map; + +import javax.sql.DataSource; + +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.DependsOn; +import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Profile; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; + +import com.zaxxer.hikari.HikariDataSource; + +import lombok.RequiredArgsConstructor; + +@Configuration +@RequiredArgsConstructor +@Profile("prod") +@EnableJpaRepositories(basePackages = "codezap") +public class DataSourceConfig { + + @Bean + @ConfigurationProperties("spring.datasource.write") + public DataSource writeDataSource() { + return DataSourceBuilder.create().type(HikariDataSource.class).build(); + } + + @Bean + @ConfigurationProperties("spring.datasource.read") + public DataSource readeDataSource() { + return DataSourceBuilder.create().type(HikariDataSource.class).build(); + } + + @Bean + @DependsOn({"writeDataSource", "readeDataSource"}) + public DataSource routeDataSource() { + DataSourceRouter dataSourceRouter = new DataSourceRouter(); + DataSource writerDataSource = writeDataSource(); + DataSource readerDataSource = readeDataSource(); + + Map dataSourceMap = new HashMap<>(); + dataSourceMap.put("write", writerDataSource); + dataSourceMap.put("read", readerDataSource); + + dataSourceRouter.setTargetDataSources(dataSourceMap); + dataSourceRouter.setDefaultTargetDataSource(writerDataSource); + + return dataSourceRouter; + } + + @Bean + @Primary + @DependsOn({"routeDataSource"}) + public DataSource dataSource() { + return new LazyConnectionDataSourceProxy(routeDataSource()); + } +} diff --git a/backend/src/main/java/codezap/global/rds/DataSourceRouter.java b/backend/src/main/java/codezap/global/rds/DataSourceRouter.java new file mode 100644 index 000000000..39980e930 --- /dev/null +++ b/backend/src/main/java/codezap/global/rds/DataSourceRouter.java @@ -0,0 +1,17 @@ +package codezap.global.rds; + +import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; +import org.springframework.transaction.support.TransactionSynchronizationManager; + +public class DataSourceRouter extends AbstractRoutingDataSource { + + @Override + protected Object determineCurrentLookupKey() { + boolean readOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); + + if(readOnly) { + return "read"; + } + return "write"; + } +} diff --git a/backend/src/main/resources/logger/logback-spring-prod.xml b/backend/src/main/resources/logger/logback-spring-prod.xml index 3dac47804..0bb0279eb 100644 --- a/backend/src/main/resources/logger/logback-spring-prod.xml +++ b/backend/src/main/resources/logger/logback-spring-prod.xml @@ -12,7 +12,8 @@ - + + From dc25796485967874bbbff503df974e2efb32850a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=AC=EC=9A=B0?= Date: Thu, 26 Sep 2024 17:26:35 +0900 Subject: [PATCH 2/4] =?UTF-8?q?refactor(service):=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EB=B0=8F=20=EC=88=98=EC=A0=95=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EC=97=90=20transactional=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/main/java/codezap/likes/service/LikesService.java | 1 + .../src/main/java/codezap/template/service/TemplateService.java | 1 + .../src/main/java/codezap/template/service/ThumbnailService.java | 1 + 3 files changed, 3 insertions(+) diff --git a/backend/src/main/java/codezap/likes/service/LikesService.java b/backend/src/main/java/codezap/likes/service/LikesService.java index 3f38f83a3..f307af0ec 100644 --- a/backend/src/main/java/codezap/likes/service/LikesService.java +++ b/backend/src/main/java/codezap/likes/service/LikesService.java @@ -21,6 +21,7 @@ public class LikesService { private final MemberRepository memberRepository; private final LikesRepository likesRepository; + @Transactional public void like(MemberDto memberDto, long templateId) { Template template = templateRepository.fetchById(templateId); Member member = memberRepository.fetchById(memberDto.id()); diff --git a/backend/src/main/java/codezap/template/service/TemplateService.java b/backend/src/main/java/codezap/template/service/TemplateService.java index 571f83474..facb56908 100644 --- a/backend/src/main/java/codezap/template/service/TemplateService.java +++ b/backend/src/main/java/codezap/template/service/TemplateService.java @@ -52,6 +52,7 @@ public Page