-
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.
feat : CQRS를 위한 @transactional(readOnly) 에 따른 Secondary DB 매핑 처리
- Loading branch information
1 parent
60fe247
commit f848e5b
Showing
3 changed files
with
84 additions
and
72 deletions.
There are no files selected for viewing
26 changes: 15 additions & 11 deletions
26
src/main/java/bc1/gream/global/config/datasource/TransactionRoutingDataSource.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 |
---|---|---|
@@ -1,15 +1,19 @@ | ||
package bc1.gream.global.config.datasource; | ||
|
||
|
||
public class TransactionRoutingDataSource { | ||
// extends AbstractRoutingDataSource { | ||
|
||
// @Nullable | ||
// @Override | ||
// protected Object determineCurrentLookupKey() { | ||
// return TransactionSynchronizationManager | ||
// .isCurrentTransactionReadOnly() ? | ||
// DataSourceType.READ_ONLY : | ||
// DataSourceType.READ_WRITE; | ||
// } | ||
import jakarta.annotation.Nullable; | ||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
public class TransactionRoutingDataSource | ||
extends AbstractRoutingDataSource { | ||
|
||
@Nullable | ||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
return TransactionSynchronizationManager | ||
.isCurrentTransactionReadOnly() ? | ||
DataSourceType.READ_ONLY : | ||
DataSourceType.READ_WRITE; | ||
} | ||
} |
99 changes: 56 additions & 43 deletions
99
src/main/java/bc1/gream/global/config/datasource/TransactionRoutingDataSourceConfig.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 |
---|---|---|
@@ -1,52 +1,65 @@ | ||
package bc1.gream.global.config.datasource; | ||
|
||
import com.zaxxer.hikari.HikariDataSource; | ||
import jakarta.persistence.EntityManagerFactory; | ||
import java.sql.SQLException; | ||
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.jdbc.datasource.LazyConnectionDataSourceProxy; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
@Configuration | ||
public class TransactionRoutingDataSourceConfig { | ||
|
||
// @ConfigurationProperties(prefix = "spring.datasource.hikari.primary") | ||
// @Bean | ||
// public DataSource primaryDataSource() { | ||
// return DataSourceBuilder.create().type(HikariDataSource.class).build(); | ||
// } | ||
// | ||
// @ConfigurationProperties(prefix = "spring.datasource.hikari.secondary") | ||
// @Bean | ||
// public DataSource secondaryDataSource() { | ||
// return DataSourceBuilder.create().type(HikariDataSource.class).build(); | ||
// } | ||
// | ||
// @DependsOn({"primaryDataSource", "secondaryDataSource"}) | ||
// @Bean | ||
// public DataSource routingDataSource( | ||
// @Qualifier("primaryDataSource") DataSource primary, | ||
// @Qualifier("secondaryDataSource") DataSource secondary) throws SQLException { | ||
// TransactionRoutingDataSource routingDataSource = new TransactionRoutingDataSource(); | ||
// | ||
// Map<Object, Object> dataSourceMap = Map.of( | ||
// DataSourceType.READ_WRITE, primary, | ||
// DataSourceType.READ_ONLY, secondary | ||
// ); | ||
// | ||
// routingDataSource.setTargetDataSources(dataSourceMap); | ||
// routingDataSource.setDefaultTargetDataSource(primary); | ||
// | ||
// 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; | ||
// } | ||
@ConfigurationProperties(prefix = "spring.datasource.hikari.primary") | ||
@Bean | ||
public DataSource primaryDataSource() { | ||
return DataSourceBuilder.create().type(HikariDataSource.class).build(); | ||
} | ||
|
||
@ConfigurationProperties(prefix = "spring.datasource.hikari.secondary") | ||
@Bean | ||
public DataSource secondaryDataSource() { | ||
return DataSourceBuilder.create().type(HikariDataSource.class).build(); | ||
} | ||
|
||
@DependsOn({"primaryDataSource", "secondaryDataSource"}) | ||
@Bean | ||
public DataSource routingDataSource( | ||
@Qualifier("primaryDataSource") DataSource primary, | ||
@Qualifier("secondaryDataSource") DataSource secondary) throws SQLException { | ||
TransactionRoutingDataSource routingDataSource = new TransactionRoutingDataSource(); | ||
|
||
Map<Object, Object> dataSourceMap = Map.of( | ||
DataSourceType.READ_WRITE, primary, | ||
DataSourceType.READ_ONLY, secondary | ||
); | ||
|
||
routingDataSource.setTargetDataSources(dataSourceMap); | ||
routingDataSource.setDefaultTargetDataSource(primary); | ||
|
||
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; | ||
} | ||
} |
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