简体   繁体   English

在Spring启动应用程序中未在多个数据库中创建表

[英]Tables not getting created in multiple databases in spring boot application

I am working on spring boot multi tenancy application. 我正在开发spring boot多租赁应用程序。 I have configured multi datasources as shown below : 我已经配置了多个数据源,如下所示:

application.properties application.properties

spring.multitenancy.datasource1.url=jdbc:mysql://localhost:3306/db1
spring.multitenancy.datasource1.username=root
spring.multitenancy.datasource1.password=****
spring.multitenancy.datasource1.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update

spring.multitenancy.datasource2.url=jdbc:mysql://localhost:3306/db2
spring.multitenancy.datasource2.username=root
spring.multitenancy.datasource2.password=****
spring.multitenancy.datasource2.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update

spring.multitenancy.datasource3.url=jdbc:mysql://localhost:3306/db3
spring.multitenancy.datasource3.username=root
spring.multitenancy.datasource3.password=****
spring.multitenancy.datasource3.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update

DataSourceBasedMultiTenantConnectionProviderImpl.java DataSourceBasedMultiTenantConnectionProviderImpl.java

@Component
public class DataSourceBasedMultiTenantConnectionProviderImpl extends AbstractDataSourceBasedMultiTenantConnectionProviderImpl {

    private static final long serialVersionUID = 8168907057647334460L;
    private static final String DEFAULT_TENANT_ID = "tenant_1";

    @Autowired
    private DataSource dataSource1;

    @Autowired
    private DataSource dataSource2;

    @Autowired
    private DataSource dataSource3;

    private Map<String, DataSource> map;

    @PostConstruct
    public void load() {
        map = new HashMap<>();
        map.put("tenant_1", dataSource1);
        map.put("tenant_2", dataSource2);
        map.put("tenant_3", dataSource3);
    }

    @Override
    protected DataSource selectAnyDataSource() {
        return map.get(DEFAULT_TENANT_ID);
    }

    @Override
    protected DataSource selectDataSource(String tenantIdentifier) {
        return map.get(tenantIdentifier);
    }
}

MultitenancyProperties.java MultitenancyProperties.java

@ConfigurationProperties("spring.multitenancy")
public class MultitenancyProperties {

    @NestedConfigurationProperty
    private DataSourceProperties datasource1;

    @NestedConfigurationProperty
    private DataSourceProperties datasource2;

    @NestedConfigurationProperty
    private DataSourceProperties datasource3;

    public DataSourceProperties getDatasource1() {
        return datasource1;
    }

    public void setDatasource1(DataSourceProperties datasource1) {
        this.datasource1 = datasource1;
    }

    public DataSourceProperties getDatasource2() {
        return datasource2;
    }

    public void setDatasource2(DataSourceProperties datasource2) {
        this.datasource2 = datasource2;
    }

    public DataSourceProperties getDatasource3() {
        return datasource3;
    }

    public void setDatasource3(DataSourceProperties datasource3) {
        this.datasource3 = datasource3;
    }
}

MultiTenancyJpaConfiguration.java MultiTenancyJpaConfiguration.java

@Configuration
@EnableConfigurationProperties(JpaProperties.class)
public class MultiTenancyJpaConfiguration {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private JpaProperties jpaProperties;

    @Autowired
    private MultiTenantConnectionProvider multiTenantConnectionProvider;

    @Autowired
    private CurrentTenantIdentifierResolver currentTenantIdentifierResolver;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        Map<String, Object> hibernateProps = new LinkedHashMap<>();
        hibernateProps.putAll(jpaProperties.getHibernateProperties(dataSource));

        hibernateProps.put(Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
        hibernateProps.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
        hibernateProps.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolver);
        hibernateProps.put(Environment.DIALECT, "org.hibernate.dialect.MySQLDialect");

        return builder.dataSource(dataSource).packages(HotelEntity.class.getPackage().getName()).properties(hibernateProps).jta(false).build();
    }
}

Application launcher 应用程序启动器

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(MultitenancyProperties.class)
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

When I run the boot application, all tables are created in only first data source. 当我运行引导应用程序时,所有表都只在第一个数据源中创建。
1) How can I create tables in all data sources on application startup? 1)如何在应用程序启动时在所有数据源中创建表?
2) How to see connections opened/closed for each of the data sources? 2)如何查看每个数据源的打开/关闭连接?
3) Is there a better way of configuring multi tenancy application using spring boot for better performance? 3)是否有更好的方法使用弹簧启动配置多租赁应用程序以获得更好的性能?

As @Alex said, you need differnt EntityManagers, TransactionManager and Datasources. 正如@Alex所说,您需要不同的EntityManagers,TransactionManager和Datasources。 Here is how I would do it 我就是这样做的

@Configuration
@EnableJpaRepositories(
    entityManagerFactoryRef = "dataSource1EntityManagerFactory",
    transactionManagerRef = "dataSource1TransactionManager",
    basePackageClasses = dataSource1Repository.class)
public class DataSource1Config extends SqlConfig{// Put all common code in base class SqlConfig. If not remove it

    @Bean
    @Primary
    public DataSource dataSource1() {
        //create dataSource using MultitenancyProperties::getDataSource1
    }

    @Primary
    @Bean(name = "dataSource1TransactionManager")
    PlatformTransactionManager dataSource1TransactionManager(EntityManagerFactory dataSource1EntityManagerFactory) {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(dataSource1EntityManagerFactory);
        return txManager;
    }

    @Primary
    @Bean(name = "dataSource1EntityManagerFactory")
    LocalContainerEntityManagerFactoryBean dataSource1EntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource1());
        em.setPackagesToScan(dataSource1Repository.class.getPackage().getName(), dataSource1ModelClass.class.getPackage().getName());
        em.setPersistenceUnitName("dataSource1Db");

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false);
        em.setJpaVendorAdapter(vendorAdapter);
        return em;
    }
}

You can create two other classes like this. 您可以创建另外两个这样的类。 Remember to use @Primary on only one instace of datasource, transactionmanger and entitymanager(doesn't matter which one). 记住只在数据源,事务管理器和实体管理器的一个@Primary上使用@Primary (无关紧要)。 Another word of caution, make sure Repository classes are in different packages for all three databases. 另一个需要注意的是,确保Repository类位于所有三个数据库的不同包中。

You need 2 different persistence factories, not one, each should produce different EntityManagers for different datasources. 您需要2个不同的持久性工厂,而不是一个,每个工厂应为不同的数据源生成不同的EntityManagers。 Also each entity mapping should be marked to been used only with one entity manager. 此外,每个实体映射都应标记为仅与一个实体管理器一起使用。 See full solution here: 请在此处查看完整解

http://www.baeldung.com/spring-data-jpa-multiple-databases http://www.baeldung.com/spring-data-jpa-multiple-databases

尝试添加以下属性:

spring.jpa.generate-ddl=true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM