Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#63 JPA multiple datasource 설정에 오류 발생하는 부분 수정 #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

humbledude
Copy link

예제는 기본으로 제공되는 CoreApplicationContext.java 에서 DB 설정하는 부분을 MainDBConfig.java 로 옮기고 나서 작업해 봤습니다.
아래와 같이 사용하게 되었을 때, SchemaGeneratorBase 에서 Autowired 로 가져오는 LocalContainerEntityManagerFactoryBean 에서 bean 을 연결하지 못해 에러가 발생했었습니다.
그래서 소스상에서 사용하지 않고 있길래 삭제하여 테스트 했더니, 잘 동작 했습니다.

이 부분을 좀더 다듬어 주셔서 Qualifier 등을 쓰거나 해도 multiple datasource 를 사용하게 되는데 더 도움이 될것 같습니다.

예제
MainDBConfig.java : AXBoot 기본 DB 포함

@Configuration
@EnableJpaRepositories(
        basePackages = GlobalConstants.DOMAIN_PACKAGE,
        entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager"
)
public class MainDBConfig {

    @Bean (name = "dataSource")
    @Primary
    public DataSource dataSource(@Named(value = "axBootContextConfig") AXBootContextConfig axBootContextConfig) throws Exception {
        return AXBootDataSourceFactory.create(axBootContextConfig.getDataSourceConfig());
    }

    @Bean (name = "entityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, AXBootContextConfig axBootContextConfig) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setPackagesToScan(axBootContextConfig.getDomainPackageName());
        AXBootContextConfig.DataSourceConfig.HibernateConfig hibernateConfig = axBootContextConfig.getDataSourceConfig().getHibernateConfig();
        entityManagerFactory.setJpaVendorAdapter(hibernateConfig.getHibernateJpaVendorAdapter());
        entityManagerFactory.setJpaProperties(hibernateConfig.getAdditionalProperties());

        return entityManagerFactory;
    }

    @Bean (name = "transactionManager")
    @Primary
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) throws ClassNotFoundException {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
        return jpaTransactionManager;
    }

    @Bean (name = "entityManager")
    @Primary
    public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
        return entityManagerFactory.createEntityManager();
    }

EventDBConfig.java : 추가한 DB

@Configuration
@EnableJpaRepositories(
        basePackages = "io.bitsound.go300.event.domain",
        entityManagerFactoryRef = "eventEntityManagerFactory",
        transactionManagerRef = "eventTransactionManager"
)
public class EventDBConfig {

    @Bean (name = "eventDataSource")
    @ConfigurationProperties(prefix = "event.dataSource")
    public DataSource eventDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean (name = "eventEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean eventEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(eventDataSource());
        em.setPackagesToScan("io.bitsound.go300.event.domain");
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);

        return em;
    }

    @Bean (name = "eventTransactionManager")
    public PlatformTransactionManager eventTransactionManager(EntityManagerFactory eventEntityManagerFactory) throws ClassNotFoundException {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(eventEntityManagerFactory);
        return jpaTransactionManager;
    }

    @Bean (name = "eventEntityManager")
    public EntityManager eventEntityManager(EntityManagerFactory eventEntityManagerFactory) {
        return eventEntityManagerFactory.createEntityManager();

    }

}

LocalContainerEntityManagerFactoryBean 제거하여 multiple datasource 설정
가능하게 함
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant