-
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.
Merge pull request #215 from Team-BC-1/refactor/rollback-to-none-cqrs
CQRS 붙이기 이전으로 롤백처리
- Loading branch information
Showing
4 changed files
with
80 additions
and
107 deletions.
There are no files selected for viewing
26 changes: 11 additions & 15 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,19 +1,15 @@ | ||
package bc1.gream.global.config.datasource; | ||
|
||
|
||
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; | ||
} | ||
public class TransactionRoutingDataSource { | ||
// extends AbstractRoutingDataSource { | ||
|
||
// @Nullable | ||
// @Override | ||
// protected Object determineCurrentLookupKey() { | ||
// return TransactionSynchronizationManager | ||
// .isCurrentTransactionReadOnly() ? | ||
// DataSourceType.READ_ONLY : | ||
// DataSourceType.READ_WRITE; | ||
// } | ||
} |
100 changes: 43 additions & 57 deletions
100
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,66 +1,52 @@ | ||
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
51 changes: 21 additions & 30 deletions
51
src/test/java/bc1/gream/global/config/datasource/TransactionRoutingDataSourceConfigTest.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,40 +1,31 @@ | ||
package bc1.gream.global.config.datasource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@SpringBootTest | ||
@Disabled("application.yml의 DB_URL과 DB_READ_ONLY_URL 를 매핑하여 테스트해야합니다.") | ||
class TransactionRoutingDataSourceConfigTest { | ||
|
||
@Autowired | ||
private DataSource dataSource; | ||
|
||
@Autowired | ||
private EntityManager entityManager; | ||
|
||
@Test | ||
@DisplayName("Read-Write Transaction에 대해 Primary DB 커넥션을 사용합니다.") | ||
@Transactional | ||
void primary_데이터소스_LazyConnectionDataSourceProxy() throws SQLException { | ||
// THEN | ||
assertTrue(dataSource.getConnection().getMetaData().getURL().contains("bc1-gream-database-01")); | ||
} | ||
|
||
@Test | ||
@DisplayName("Read-Only Transaction에 대해 Secondary DB 커넥션을 사용합니다.") | ||
@Transactional(readOnly = true) | ||
void secondary_데이터소스_LazyConnectionDataSourceProxy() throws SQLException { | ||
// THEN | ||
assertTrue(dataSource.getConnection().getMetaData().getURL().contains("bc1-gream-read-only")); | ||
} | ||
// @Autowired | ||
// private DataSource dataSource; | ||
// | ||
// @Autowired | ||
// private EntityManager entityManager; | ||
// | ||
// @Test | ||
// @DisplayName("Read-Write Transaction에 대해 Primary DB 커넥션을 사용합니다.") | ||
// @Transactional | ||
// void primary_데이터소스_LazyConnectionDataSourceProxy() throws SQLException { | ||
// // THEN | ||
// assertTrue(dataSource.getConnection().getMetaData().getURL().contains("bc1-gream-database-01")); | ||
// } | ||
// | ||
// @Test | ||
// @DisplayName("Read-Only Transaction에 대해 Secondary DB 커넥션을 사용합니다.") | ||
// @Transactional(readOnly = true) | ||
// void secondary_데이터소스_LazyConnectionDataSourceProxy() throws SQLException { | ||
// // THEN | ||
// assertTrue(dataSource.getConnection().getMetaData().getURL().contains("bc1-gream-read-only")); | ||
// } | ||
} |