티스토리 뷰

복수 데이터소스 설정하기 

스프링부트에서 하나의 데이터소스에 대한 처리는 application.properties 설정만으로 쉽게 사용할 수 있습니다. 

그러나 두개 이상의 데이터소스를 사용하고자 할때는 약간의 설정이 필요합니다. 

작업은 application.properties에 연결 정보를 추가해주고 Cofiguration class를 생성해주면 됩니다. 


spring boot 2.1.1 기반, MyBatis 사용

1. application.properties 값 추가하기 

1
2
3
4
5
6
7
8
9
10
11
12
13
spring.primary.datasource.jdbc-url=jdbc:oracle:thin:@localhost:1521:TEST1
spring.primary.datasource.username=PRIMARY
spring.primary.datasource.password=PRIMARYPWD
spring.primary.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.primary.datasource.validation-query=SELECT 1 FROM DUAL
spring.primary.datasource.default-isolation-level=DEFAULT
 
spring.secondary.datasource.jdbc-url=jdbc:oracle:thin:@localhost:1521:TEST2
spring.secondary.datasource.username=SECONDARY
spring.secondary.datasource.password=SECONDARYPWD
spring.secondary.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.secondary.datasource.validation-query=SELECT 1 FROM DUAL
spring.secondary.datasource.default-isolation-level==DEFAULT
cs

 

2. Configuration 클래스 생성

PrimaryDatasource

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import javax.sql.DataSource;
 
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
 
@Configuration
public class PrimaryDatasource {
 
    @Primary
    @Bean(name="dataSource")
    @ConfigurationProperties(prefix="spring.primary.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
 
    @Primary
    @Bean(name="sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(@Autowired @Qualifier("dataSource") DataSource dataSource, ApplicationContext applicationContext)
            throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setConfigLocation(applicationContext.getResource("classpath:mybatis-config-primary.xml"));
        factoryBean.setMapperLocations(applicationContext.getResources("classpath:mapper-primary/**/*.xml"));
        return factoryBean.getObject();
    }
 
    @Primary
    @Bean(name="sqlSession")
    public SqlSessionTemplate sqlSession(@Autowired @Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
 
    @Primary
    @Bean(name="transactionManager")
    public DataSourceTransactionManager transactionManager(@Autowired @Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
 
}
cs


SecondaryDatasource

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import javax.sql.DataSource;
 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
 
@Configuration
public class SecondaryDatasource {
 
    @Bean(name="secondaryDataSource")
    @ConfigurationProperties(prefix="spring.secondary.datasource")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
 
    @Bean(name="secondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactoryBean(@Autowired @Qualifier("secondaryDataSource") DataSource secondaryDataSource, ApplicationContext applicationContext)
            throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(secondaryDataSource);
        factoryBean.setConfigLocation(applicationContext.getResource("classpath:mybatis-config-secondary.xml"));
        factoryBean.setMapperLocations(applicationContext.getResources("classpath:mapper-secondary/**/*.xml"));
        return factoryBean.getObject();
    }
    
    @Bean(name="secondarySqlSession")
    public SqlSession secondarySqlSession(@Autowired @Qualifier("secondarySqlSessionFactory") SqlSessionFactory secondarySqlSessionFactory) {
        return new SqlSessionTemplate(secondarySqlSessionFactory);
    }
     
    @Bean(name="secondaryTransactionManager")
    public DataSourceTransactionManager secondaryTransactionManager(@Autowired @Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
        return new DataSourceTransactionManager(secondaryDataSource);
    }
 
}
cs


사용예제

1
2
3
4
5
6
7
8
9
10
11
12
@Repository
public class UserRepository {
 
    @Autowired
    @Qualifier("secondarySqlSession")
    private SqlSession secondarySqlSession;
    
    public List<User> getUserList(User pUser) {
        return unidomSqlSession.selectList("eblo.user.getUserList", pUser);
    }
 
}
cs


Primary는 이름 지정을 하지 않아도 됩니다. 


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함