Created
February 6, 2018 09:06
-
-
Save jaceshim/adca2e2fa53fee30d016444db4e729c3 to your computer and use it in GitHub Desktop.
Multiple DataSource on springboot
This file contains hidden or 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
package com.kakao.order.common.config.database; | |
/** | |
* Created by jaceshim on 2017. 8. 23.. | |
*/ | |
public enum DataSourceType { | |
WRITE, READ | |
} |
This file contains hidden or 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
package com.kakao.order.common.config.database; | |
import javax.sql.DataSource; | |
import java.util.HashMap; | |
import java.util.Map; | |
import com.zaxxer.hikari.HikariDataSource; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; | |
/** | |
* Created by jaceshim on 2017. 8. 23.. | |
*/ | |
@Configuration | |
public class OrderDataSourceConfig { | |
private Logger log = LoggerFactory.getLogger(OrderDataSourceConfig.class); | |
@Bean(destroyMethod = "shutdown") | |
public DataSource readDataSource() { | |
// TODO: read용 datasource얻기. | |
return null; | |
} | |
@Bean(destroyMethod = "shutdown") | |
public DataSource writeDataSource() { | |
// TODO: write용 datasource얻기. | |
return null; | |
} | |
@Bean | |
public DataSource routingDataSource(DataSource writeDataSource, DataSource readDataSource) { | |
RoutingDataSource routingDataSource = new RoutingDataSource(); | |
Map<Object, Object> dataSourceMap = new HashMap<>(); | |
dataSourceMap.put(DataSourceType.WRITE.name(), writeDataSource); | |
dataSourceMap.put(DataSourceType.READ.name(), readDataSource); | |
routingDataSource.setTargetDataSources(dataSourceMap); | |
routingDataSource.setDefaultTargetDataSource(writeDataSource); | |
return routingDataSource; | |
} | |
@Bean | |
public DataSource dataSource(DataSource routingDataSource) { | |
return new LazyConnectionDataSourceProxy(routingDataSource); | |
} | |
} |
This file contains hidden or 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
package com.kakao.order.common.config.database; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | |
import org.springframework.transaction.support.TransactionSynchronizationManager; | |
/** | |
* {@link org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource}와 | |
* {@link org.springframework.transaction.support.TransactionSynchronizationManager}를 통해 | |
* Transaction의 readOnly 값에 따라 DataSource를 선택 | |
*/ | |
public class RoutingDataSource extends AbstractRoutingDataSource { | |
private Logger log = LoggerFactory.getLogger(RoutingDataSource.class); | |
@Override | |
protected Object determineCurrentLookupKey() { | |
DataSourceType dataSourceType | |
= TransactionSynchronizationManager.isCurrentTransactionReadOnly() ? DataSourceType.READ : DataSourceType.WRITE; | |
log.info("current data source type : {}", dataSourceType.name()); | |
return dataSourceType; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment