Skip to content

Commit

Permalink
Merge pull request #215 from Team-BC-1/refactor/rollback-to-none-cqrs
Browse files Browse the repository at this point in the history
CQRS 붙이기 이전으로 롤백처리
  • Loading branch information
Binsreoun authored Jan 30, 2024
2 parents 664b242 + c08b792 commit 479088e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 107 deletions.
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;
// }
}
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;
// }

}
10 changes: 5 additions & 5 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ spring:
jdbc-url: jdbc:mysql://${DB_URL}/gream
username: ${DB_ID}
password: ${DB_PASSWORD}
secondary:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://${DB_READ_ONLY_URL}/gream
username: ${DB_ID}
password: ${DB_PASSWORD}
# secondary:
# driver-class-name: com.mysql.cj.jdbc.Driver
# jdbc-url: jdbc:mysql://${DB_READ_ONLY_URL}/gream
# username: ${DB_ID}
# password: ${DB_PASSWORD}
# JPA
jpa:
hibernate:
Expand Down
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"));
// }
}

0 comments on commit 479088e

Please sign in to comment.