spring-boot应用程序不会将灯具加载到多个数据源之一

时间:2017-03-30 13:31:15

标签: spring postgresql spring-boot h2

在启动春季启动应用程序(1.5.2.RELEASE)期间加载数据夹具时遇到一些问题。该应用程序使用两个不同的数据库连接,一个连接到我们的客户postgresql数据库,我们没有权限创建,插入或更新任何内容。另一个数据库是本地嵌入式h2数据库(文件)。我希望在应用程序启动期间通过使用如here所述的spring boot数据库初始化阶段将一些数据加载到该h2数据库中,但数据永远不会插入到h2数据库中,它会保持为空,因为我可以看到使用松鼠-SQL

这是我的application.properties中的配置:

spring.datasource.abc.driver-class-name=org.postgresql.Driver
spring.datasource.abc.initialize=false
spring.datasource.abc.url=jdbc:postgresql://localhost:5432/abc
spring.datasource.abc.username=abc
spring.datasource.abc.password=abc

spring.datasource.def.driver-class-name=org.h2.Driver
spring.datasource.def.initialize=true
spring.datasource.def.url=jdbc:h2:./${path.prefix}def/def;DB_CLOSE_ON_EXIT=FALSE'
spring.datasource.def.data=classpath:/data-h2.sql

我将spring boot配置为使用两个不同的数据库,如本stackoverflow post中所述,如果我手动将数据预先插入到h2数据库中,这一切都可以正常工作。

配置postgresql数据源:

@Configuration
@EnableJpaRepositories(basePackages = "....abc", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
public class AbcDatabaseConfig
{

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.abc")
    public DataSource dataSource()
    {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory()
    {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan("....abc");
        HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl-auto", "none");
        properties.put("hibernate.ejb.entitymanager_factory_name", "entityManagerFactory");
        em.setJpaPropertyMap(properties);

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

    @Primary
    @Bean(name = "transactionManager")
    public JpaTransactionManager transactionManager(@Qualifier("entityManagerFactory") final EntityManagerFactory factory)
    {
        return new JpaTransactionManager(factory);
    }

}

配置h2-datasource:

@Configuration
@EnableJpaRepositories(basePackages = "....def", entityManagerFactoryRef = "defEntityManagerFactory", transactionManagerRef = "defTransactionManager")
public class InavetDatabaseConfig
{

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

    @Bean(name = "defEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean defEntityManagerFactory()
    {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(defDataSource());
        em.setPackagesToScan("....def");

        HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl.auto", "create");
        properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        properties.put("hibernate.ejb.entitymanager_factory_name", "defEntityManagerFactory");
        em.setJpaPropertyMap(properties);

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

    @Bean(name = "defTransactionManager")
    public JpaTransactionManager defTransactionManager(
            @Qualifier("defEntityManagerFactory") final EntityManagerFactory factory)
    {
        return new JpaTransactionManager(factory);
    }

}

1 个答案:

答案 0 :(得分:0)

我发现,只有@Primary标记的数据源加载了固定装置。我对此行为的解决方法是将这样的代码添加到我的应用程序中:

    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.setContinueOnError(true);
    populator.addScript(new PathResource("src/main/resources/data-h2.sql"));
    DataSource dataSource = (DataSource) cac.getBean("defDataSource");
    DatabasePopulatorUtils.execute(populator, dataSource);

其中cac是ConfigurableApplicationContext的实例,你得到的返回值为:SpringApplication.run(,);