Spring Boot 中的多数据源和模式创建 [英] Multiple data source and schema creation in Spring Boot

查看:31
本文介绍了Spring Boot 中的多数据源和模式创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Boot.我终于设法设置了两个数据源,但现在我面临另一个问题.

I'm using Spring Boot. I finally managed to setup two data sources, but now I'm facing another issue.

  1. 有两个数据源 spring.jpa.hibernate.ddl-auto=create 似乎在我的 Spring Boot 应用程序中停止工作,只有 spring.jpa.generate-ddl=true 立即开始工作

  1. with two data sources in place spring.jpa.hibernate.ddl-auto=create seems to stop working in my spring boot application, only spring.jpa.generate-ddl=true do the job now

我无法为每个数据源选择自动创建策略.我更愿意为数据源一创建架构,并只使用第二个数据库中创建的架构和数据源二.

I can not manage to select the auto-creation strategy for each of the data sources. I would prefer to create the schema for data source one, and just use the created schema in second DB with data source two.

有人知道如何解决这些问题吗?请注意,如果可能,我不想完全丢弃自动配置.我什至不知道,如果 hibernate 能够在一个持久性单元中初始化架构.

Any body have idea how to resolve any of these issues? Note I don't want to completely throw away the auto-config if possible. I don't even know yet, if hibernate is able to just initialize schema in one persistence unit.

application.properties

application.properties

spring.datasource-internal.url=jdbc:hsqldb:mem:testdb
spring.datasource-internal.username=sa
spring.datasource-internal.password=sa
spring.datasource-internal.driver-class-name=org.hsqldb.jdbcDriver
spring.datasource-internal.jpa.database-platform=org.hibernate.dialect.HSQLDialect

spring.datasource-external.url=jdbc:hsqldb:mem:testexternal
spring.datasource-external.username=sa
spring.datasource-external.password=sa
spring.datasource-external.driver-class-name=org.hsqldb.jdbcDriver
spring.datasource-external.jpa.database-platform=org.hibernate.dialect.HSQLDialect

flyway.enabled=false
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true

DBInternalConfig

DBInternalConfig


    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(basePackages = "cz.data.internal",
            entityManagerFactoryRef = "internalEntityManagerFactory",
            transactionManagerRef = "internalTransactionManager")
    public class DBConfigInternal {


        public static final String INTERNAL = "internal";

        @Bean(name = "internalDataSource")
        @Primary
        @ConfigurationProperties(prefix = "spring.datasource-internal")
        public DataSource internalDataSource() {
            return DataSourceBuilder.create().build();
        }

        @Bean(name = "internalEntityManagerFactory")
        @Primary
        public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory(
                EntityManagerFactoryBuilder builder) {
            return builder
                    .dataSource(internalDataSource())
                    .packages("cz.data.internal.entity")
                    .persistenceUnit(INTERNAL)
                    .build();
        }

        @Bean(name = "internalTransactionManager")
        @Primary
        public PlatformTransactionManager internalTransactionManager() {
            JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
            jpaTransactionManager.setDataSource(internalDataSource());
            jpaTransactionManager.setPersistenceUnitName(INTERNAL);
            return jpaTransactionManager;
        }
    }

DBExternalConfig

DBExternalConfig


    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
            basePackages = "cz.data.external",
            entityManagerFactoryRef = "externalEntityManagerFactory",
            transactionManagerRef = "externalTransactionManager")
    public class DBConfigExternal {


        public static final String EXTERNAL = "external";

        @Bean(name = "externalDataSource")
        @ConfigurationProperties(prefix = "spring.datasource-external")
        public DataSource externalDataSource() {
            return DataSourceBuilder.create().build();
        }

        @Bean(name = "externalEntityManagerFactory")
        public LocalContainerEntityManagerFactoryBean externalEntityManagerFactory(
                EntityManagerFactoryBuilder builder) {
            return builder
                    .dataSource(externalDataSource())
                    .packages("cz.data.external.entity")
                    .persistenceUnit(EXTERNAL)
                    .build();
        }

        @Bean(name = "externalTransactionManager")
        public PlatformTransactionManager externalTransactionManager() {
            JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
            jpaTransactionManager.setDataSource(externalDataSource());
            jpaTransactionManager.setPersistenceUnitName(EXTERNAL);
            return jpaTransactionManager;
        }
    }

M.W.

推荐答案

spring.jpa.hibernate.ddl-auto=create 已停止工作,不是因为您有两个 DataSource,而是因为您的应用程序的创建自己的 LocalContainerEntityManagerFactoryBeans.这会禁用 LocalContainerEntityManagerFactoryBean 的自动配置,因此您现在必须自己配置它.

spring.jpa.hibernate.ddl-auto=create has stopped working, not because you have two DataSources, but because your application's creating its own LocalContainerEntityManagerFactoryBeans. This has the effect of disabling the auto-configuration of a LocalContainerEntityManagerFactoryBean so you now have to configure it yourself.

您可以将两个实体管理器配置为具有不同的模式生成行为,如下所示(第一个进行更新,第二个进行创建):

You can configure the two entity managers to have different schema generation behaviour like this (the first's doing update, the second's doing create):

@Bean(name = "externalEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean externalEntityManagerFactory(
        EntityManagerFactoryBuilder builder) {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("hibernate.hbm2ddl.auto", "update");
    return builder
            .dataSource(externalDataSource())
            .packages("cz.data.external.entity")
            .persistenceUnit(EXTERNAL)
            .properties(properties)
            .build();
}

@Bean(name = "internalEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean internalEntityManagerFactory(
        EntityManagerFactoryBuilder builder) {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("hibernate.hbm2ddl.auto", "create");
    return builder
            .dataSource(internalDataSource())
            .packages("cz.data.internal.entity")
            .persistenceUnit(INTERNAL)
            .properties(properties)
            .build();
}

这篇关于Spring Boot 中的多数据源和模式创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆