일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- 사운드 설치
- IOS
- 코로나
- Linux
- 내장모니터끄기
- ㅂㅅ 같은 삼성 노트북
- aws
- 포켓몬고 플러스
- tomcat
- 갤럭시북 사운드카드 설치
- 로드밸런싱
- 광고없이 보기
- java
- 앞으로 가기
- 미세먼지
- 포켓몬 고
- ubuntu-server
- 포켓몬고
- 유투브 광고 우회
- lb
- 공기청정기
- 미국은 또 시궁창
- 초미세먼지
- 크롬
- PM 2.5
- 클러스터링
- Docker
- OSX
- 마스크
- 노트북 모니터 끄기
- Today
- Total
살며사랑하며
springboot multi datasource hikaricp 설정 본문
routingdatasource를 이용해서 다중 db 접속을 할때 hikaricp를 이용해서 접속설정을 하게 되면 hikaricp의 상세 설정이 적용되지 않는다.
해당 설정을 바꾸기 위해서는
@Bean
@ConfigurationProperties(prefix = "spring.ds2.hikari")
public HikariConfig hikariConfigDB1() {
return new HikariConfig();
}
@Bean
@ConfigurationProperties(prefix = "spring.ds2")
public DataSource dataSourceDB1() {
return new HikariDataSource(hikariConfigDB1());
}
해당 과정을 통해서 hikaricp의 설정을 들고오게끔 변경해줘야 한다.
DataSourceType .java
public enum DataSourceType {
DB1, DB2
}
DataSource .java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSource {
DataSourceType value() default DataSourceType.DB1;
}
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class RoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return ContextHolder.getDataSourceType();
}
}
RoutingDataSource .java
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class RoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return ContextHolder.getDataSourceType();
}
}
DataSourceConfig .java
package com.DB2.config;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
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.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.ds2.hikari")
public HikariConfig hikariConfigDB1() {
return new HikariConfig();
}
@Bean
@ConfigurationProperties(prefix = "spring.ds2")
public DataSource dataSourceDB1() {
return new HikariDataSource(hikariConfigDB1());
}
@Bean
@ConfigurationProperties(prefix = "spring.ds1.hikari")
public HikariConfig hikariConfigDB2() {
return new HikariConfig();
}
@Bean
@ConfigurationProperties(prefix = "spring.ds1")
public DataSource dataSourceDB2() {
return new HikariDataSource(hikariConfigDB2());
}
@Bean
@Primary
public DataSource dataSource() {
Map<Object, Object> targetDatasources = new HashMap<Object, Object>() {
private static final long serialVersionUID = 4740238371195333169L;
{
put(DataSourceType.DB1, dataSourceDB1());
put(DataSourceType.DB2, dataSourceDB2());
}
};
RoutingDataSource routingDataSource = new RoutingDataSource();
routingDataSource.setDefaultTargetDataSource(dataSourceDB1());
routingDataSource.setTargetDataSources(targetDatasources);
routingDataSource.afterPropertiesSet();
return routingDataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean(name="sqlSessionFactoryDB1")
public SqlSessionFactory sqlSessionFactory(@Autowired @Qualifier("dataSourceDB1") DataSource dataSource)
throws Exception {
logger.debug("sqlSessionFactory", dataSource);
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/sqlite1/*.xml"));
String mybatisXml = null;
sqlSessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResources(mybatisXml)[0]);
return sqlSessionFactoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionDB1(@Autowired @Qualifier("sqlSessionFactoryDB1") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactoryDB);
}
@Bean(name="sqlSessionFactoryDB2")
public SqlSessionFactory sqliteSessionFactoryDB2(@Autowired @Qualifier("dataSourceDB2") DataSource dataSource)
throws Exception {
logger.debug("sqlSessionFactory", dataSource);
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/sqlite2/*.xml"));
sqlSessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResources(mybatisXml)[0]);
return sqlSessionFactoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionDB2(@Autowired @Qualifier("sqlSessionFactoryDB2") SqlSessionFactory factory) {
return new SqlSessionTemplate(factory);
}
}
application.properties
spring.datasource.initialize=true
# DB1
spring.ds1.hikari.driver-class-name=org.sqlite.JDBC
spring.ds1.hikari.jdbc-url=jdbc:sqlite:sqlite1.db
#spring.ds1.hikari.username=user
#spring.ds1.hikari.password=password
spring.ds1.hikari.minimumIdle=20
spring.ds1.hikari.maximumPoolSize=100
spring.ds1.hikari.idleTimeout=60000
spring.ds1.hikari.connectionTimeout=30000
spring.ds1.hikari.connectionTestQuery=SELECT 0
# DB2
spring.ds2.hikari.driver-class-name=org.sqlite.JDBC
spring.ds2.hikari.jdbc-url=jdbc:sqlite:sqlite2.db
#spring.ds2.hikari.username=user
#spring.ds2.hikari.password=password
spring.ds2.hikari.minimumIdle=20
spring.ds2.hikari.maximumPoolSize=100
spring.ds2.hikari.idleTimeout=60000
spring.ds2.hikari.connectionTimeout=30000
spring.ds2.hikari.connectionTestQuery=SELECT 0
call from service
public class TestService {
@Autowired
@Resource(name="sqlSessionDB1")
private SqlSession sqlSessionDB1;
@Autowired
@Resource(name="sqlSessionDB2")
private SqlSession sqlSessionDB2;
blah;
blah;
blah;
}