카테고리 없음
JPA MULTI DATABASE 설정
Potwings
2022. 7. 22. 14:06
리뉴얼을 진행하면서 AS-IS 데이터를 읽고 쓸 필요가 생겼다.
AS-IS의 경우 Avalon 프레임워크로 되어있어 API를 개발하자니 무리가 있어 직접 DB에 연결하여 작업하기로 하였다.
이에 따라 TO-BE에서 데이터베이스 두개를 바라봐야하게 되었다.
이는 DataSourceConfig를 아래와 같이 DataSource를 두개 잡아줌으로써 해결되었다.
application.yml
spring:
devtools:
livereload:
enabled: true
jpa:
show-sql: true
hibernate:
format_sql: true
ddl-auto: update
show-sql: true
datasource:
driver-class-name: org.mariadb.jdbc.Driver
url: jdbc:mariadb://DB1아이피:포트/스키마
username: DB1 계정
password: DB1 PW
secondary-datasource:
driver-class-name: org.mariadb.jdbc.Driver
url: jdbc:mariadb://DB2아이피:포트/스키마
username: DB2 계정
password: DB2 PW
PrimaryDataSourceConfig.java
@Configuration
@PropertySource("classpath:application.yml")
@EnableJpaRepositories(
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager",
basePackages = {"com.example.demo.tobe.repository"} //Repository 경로
)
public class PrimaryDataSourceConfig {
@Autowired
private Environment env;
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(primaryDataSource());
//Entity 패키지 경로
em.setPackagesToScan(new String[]{"com.example.demo.tobe.entity"});
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
//Hibernate 설정
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Primary
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource primaryDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));
return dataSource;
}
@Primary
@Bean
public PlatformTransactionManager primaryTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(primaryEntityManagerFactory().getObject());
return transactionManager;
}
}
SecondaryDataSourceConfig.java
@Configuration
@PropertySource("classpath:application.yml")
@EnableJpaRepositories(
entityManagerFactoryRef = "secondaryEntityManagerFactory",
transactionManagerRef = "secondaryTransactionManager",
basePackages = {"com.example.demo.asis.repository"} //Repository 경로
)
public class SecondaryDataSourceConfig {
@Autowired
private Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(secondaryDataSource());
//Entity 패키지 경로
em.setPackagesToScan(new String[]{"com.example.demo.asis.entity"});
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
//Hibernate 설정
HashMap<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
em.setJpaPropertyMap(properties);
return em;
}
@Bean
public DataSource secondaryDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.secondary-datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.secondary-datasource.url"));
dataSource.setUsername(env.getProperty("spring.secondary-datasource.username"));
dataSource.setPassword(env.getProperty("spring.secondary-datasource.password"));
return dataSource;
}
@Bean
public PlatformTransactionManager secondaryTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(secondaryEntityManagerFactory().getObject());
return transactionManager;
}
}