-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] - RDS reader, writer 엔드포인트 연결 쓰기 작업, 읽기 작업 분리 검토 (#441)
* feat: 프로덕션 프로파일 datasource RDS로 변경 * feat: 프로덕션 datasource routing Configuration 작성 * refactor: 의미 있는 문자열 상수화 * fix: LoginService 트랜잭셔널 검토 * fix: TravelPlanService 트랜잭션 속성 검토 * refactor: 사용하지 않는 애너테이션 제거 개선
- Loading branch information
Showing
6 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
backend/src/main/java/kr/touroot/global/config/DataSourceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package kr.touroot.global.config; | ||
|
||
import com.zaxxer.hikari.HikariDataSource; | ||
import jakarta.persistence.EntityManagerFactory; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.sql.DataSource; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
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.jdbc.datasource.LazyConnectionDataSourceProxy; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
@Configuration | ||
@Profile("prod") | ||
public class DataSourceConfig { | ||
|
||
public static final String WRITER = "writer"; | ||
public static final String READER = "reader"; | ||
|
||
@ConfigurationProperties(prefix = "spring.datasource.writer") | ||
@Bean | ||
public DataSource writerDataSource() { | ||
return DataSourceBuilder.create().type(HikariDataSource.class).build(); | ||
} | ||
|
||
@ConfigurationProperties(prefix = "spring.datasource.reader") | ||
@Bean | ||
public DataSource readerDataSource() { | ||
return DataSourceBuilder.create().type(HikariDataSource.class).build(); | ||
} | ||
|
||
@DependsOn({"writerDataSource", "readerDataSource"}) | ||
@Bean | ||
public DataSource routingDataSource( | ||
@Qualifier("writerDataSource") DataSource writer, | ||
@Qualifier("readerDataSource") DataSource reader) { | ||
DynamicRoutingDataSource routingDataSource = new DynamicRoutingDataSource(); | ||
|
||
Map<Object, Object> dataSourceMap = new HashMap<>(); | ||
|
||
dataSourceMap.put(WRITER, writer); | ||
dataSourceMap.put(READER, reader); | ||
|
||
routingDataSource.setTargetDataSources(dataSourceMap); | ||
routingDataSource.setDefaultTargetDataSource(writer); | ||
|
||
return routingDataSource; | ||
} | ||
|
||
@DependsOn({"routingDataSource"}) | ||
@Primary | ||
@Bean | ||
public DataSource dataSource(DataSource routingDataSource) { | ||
return new LazyConnectionDataSourceProxy(routingDataSource); | ||
} | ||
|
||
@Bean | ||
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { | ||
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); | ||
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory); | ||
return jpaTransactionManager; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/kr/touroot/global/config/DynamicRoutingDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package kr.touroot.global.config; | ||
|
||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
public class DynamicRoutingDataSource extends AbstractRoutingDataSource { | ||
|
||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) { | ||
return DataSourceConfig.READER; | ||
} | ||
return DataSourceConfig.WRITER; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters