0

I have some trouble loading data fixtures during start of my spring boot application (1.5.2.RELEASE). The application uses two different database connections, one to our customers postgresql database on which we do not have permissions to create, insert or update anything. The other database is a local embedded h2 database (file). I want to load some data during application start into that h2 database by using the spring boot database initialization phase as described here but the data is never inserted into the h2 database, it stays empty as I can see using squirrel-sql.

This is the configuration in my 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

I configured spring boot to use two different databases like described in this stackoverflow post and it all works fine if I pre-insert data by hand into the h2 database.

Configuration of my postgresql datasource:

@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);
    }

}

Configuration of 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);
    }

}
Community
  • 1
  • 1
Del Pedro
  • 1,216
  • 12
  • 32

1 Answers1

0

I found out, that only the @Primary marked data sources load fixtures. My workaround for this behaviour is adding code like this to my application:

    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);

Where cac is the instance of ConfigurableApplicationContext you get as return value by of: SpringApplication.run(,);

Del Pedro
  • 1,216
  • 12
  • 32