Skip to content

Commit

Permalink
Multi Datasource 설정 코드 추가 (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
cire0304 committed Nov 6, 2023
1 parent 66d7180 commit 5e3639c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,80 @@
package com.ayucoupon.common.config.datasource;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import com.ayucoupon.common.aop.multidatasource.MyRoutingDataSource;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableConfigurationProperties(DataSourceConfigProperties.class)
@RequiredArgsConstructor
public class DataSourceConfig {

private final DataSourceConfigProperties properties;

@Bean
public DataSource dataSource() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName(properties.getDriverClassName());
hikariConfig.setJdbcUrl(properties.getUrl());
hikariConfig.setUsername(properties.getUserName());
hikariConfig.setPassword(properties.getPassword());
hikariConfig.setMaximumPoolSize(properties.getMaximumPoolSize());
return new HikariDataSource(hikariConfig);
private final String PRIMARY_DATA_SOURCE = "primary";
private final String SECONDARY_DATA_SOURCE = "secondary";
private final String ROUTING_DATA_SOURCE = "routingDataSource";

@Bean(PRIMARY_DATA_SOURCE)
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(SECONDARY_DATA_SOURCE)
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(ROUTING_DATA_SOURCE)
@Primary
@DependsOn({PRIMARY_DATA_SOURCE, SECONDARY_DATA_SOURCE})
public DataSource routingDataSource(
@Qualifier(PRIMARY_DATA_SOURCE) DataSource primary,
@Qualifier(SECONDARY_DATA_SOURCE) DataSource secondary) {
MyRoutingDataSource routingDataSource = new MyRoutingDataSource();

Map<Object, Object> dataSourceMap = new HashMap<>();

dataSourceMap.put(PRIMARY_DATA_SOURCE, primary);
dataSourceMap.put(SECONDARY_DATA_SOURCE, secondary);

routingDataSource.setTargetDataSources(dataSourceMap);
routingDataSource.setDefaultTargetDataSource(primary);

return routingDataSource;
}

@Bean("entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
@Qualifier(ROUTING_DATA_SOURCE) DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManagerFactory
= new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPackagesToScan("com.ayucoupon");
entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactory.setPersistenceUnitName("entityManager");
return entityManagerFactory;
}

@Bean("transactionManager")
public PlatformTransactionManager platformTransactionManager(
@Qualifier("entityManagerFactory") LocalContainerEntityManagerFactoryBean entityManagerFactory) {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
return jpaTransactionManager;
}

}

This file was deleted.

15 changes: 15 additions & 0 deletions src/main/java/com/ayucoupon/common/config/warmup/AopConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ayucoupon.common.config.warmup;

import com.ayucoupon.common.aop.multidatasource.DataSourceAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AopConfig {

@Bean
public DataSourceAspect dataSourceAspect() {
return new DataSourceAspect();
}

}

0 comments on commit 5e3639c

Please sign in to comment.