-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
20 additions
and
8 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
6 changes: 6 additions & 0 deletions
6
backend/src/main/java/hanglog/global/config/datasource/DataSourceType.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,6 @@ | ||
package hanglog.global.config.datasource; | ||
|
||
public enum DataSourceType { | ||
|
||
SOURCE, REPLICA | ||
} |
15 changes: 9 additions & 6 deletions
15
backend/src/main/java/hanglog/global/config/datasource/RoutingDataSource.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,22 +1,25 @@ | ||
package hanglog.global.config.datasource; | ||
|
||
import static hanglog.global.config.datasource.DataSourceType.REPLICA; | ||
import static hanglog.global.config.datasource.DataSourceType.SOURCE; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.transaction.support.TransactionSynchronizationManager; | ||
|
||
@Slf4j | ||
public class RoutingDataSource extends AbstractRoutingDataSource { | ||
|
||
@Override | ||
protected Object determineCurrentLookupKey() { | ||
final String currentTransactionName = TransactionSynchronizationManager.getCurrentTransactionName(); | ||
final boolean isReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); | ||
logger.info(currentTransactionName + " Transaction Read Only가 " + isReadOnly + " 입니다."); | ||
|
||
if (isReadOnly) { | ||
logger.info("Replica 서버로 요청합니다."); | ||
return "replica"; | ||
log.info(currentTransactionName + " Transaction:" + "Replica 서버로 요청합니다."); | ||
return REPLICA; | ||
} | ||
|
||
logger.info("Source 서버로 요청합니다."); | ||
return "source"; | ||
log.info(currentTransactionName + " Transaction:" + "Source 서버로 요청합니다."); | ||
return SOURCE; | ||
} | ||
} |