title: Multiple data source and schema creation in Spring Boot
body: <p>I'm using Spring Boot. I finally managed to setup two data sources, but now I'm facing another issue. </p>
  
  <ol>
  <li><p>with two data sources in place <code>spring.jpa.hibernate.ddl-auto=create</code> seems to stop working in my spring boot application, only <code>spring.jpa.generate-ddl=true</code> do the job now</p></li>
  <li><p>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. </p></li>
  </ol>
  
  <p>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.</p>
  
  <p>application.properties</p>
  
  <pre class="lang-html prettyprint-override"><code>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
  </code></pre>
  
  <p>DBInternalConfig</p>
  
  <pre class="lang-java prettyprint-override"><code>
      @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;
          }
      }
  </code></pre>
  
  <p>DBExternalConfig</p>
  
  <pre class="lang-java prettyprint-override"><code>
      @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;
          }
      }
  </code></pre>
  
  <p>M.W.</p>