title: Spring AbstractRoutingDataSource + Hibernate- Hbm2ddlSchemaUpdate is executed only on default DB
body: <p>I'm using SpringBoot, along with Hibernate as persistence provider.
  For my application, I required to dynamically choose between 2 DB's.</p>
  
  <pre><code>(For simplicity sake, 
    domain : localhost:8080 ---&gt; hem1 DB
    domain : 127.0.0.1:8080 ---&gt; hem2 DB
  )
  </code></pre>
  
  <p>Following is the implementation of AbstractRoutingDB</p>
  
  <pre><code>    public class MyRoutingDataSource extends AbstractRoutingDataSource{
          @Override
          protected Object determineCurrentLookupKey() {
  
          /*
           * this is derived from threadlocal set by filter for each web           
           * request
           */
               return SessionUtil.getDB(); 
          }
      }
  </code></pre>
  
  <p>Following is the DB Configuration:</p>
  
  <pre><code>  package com.hemant.basic.dataSource;
  
      import java.beans.PropertyVetoException;
      import java.util.HashMap;
      import java.util.Map;
  
      import javax.naming.ConfigurationException;
      import javax.sql.DataSource;
  
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
  
      import com.mchange.v2.c3p0.ComboPooledDataSource;
  
      @Configuration
      public class DBConfig {
  
          @Bean(name = "dataSource")
          public DataSource dataSource() throws PropertyVetoException,
                  ConfigurationException {
              MyRoutingDataSource routingDB = new MyRoutingDataSource();
              Map&lt;Object, Object&gt; targetDataSources = datasourceList();
  
  // hem1 is the default target DB            
  routingDB.setDefaultTargetDataSource(targetDataSources.get(1));
              routingDB.setTargetDataSources(targetDataSources);
              routingDB.afterPropertiesSet();
              return routingDB;
          }
  
          private Map&lt;Object, Object&gt; datasourceList() throws PropertyVetoException,
                  ConfigurationException {
              final Map&lt;Object, Object&gt; datasources = new HashMap&lt;Object, Object&gt;();
              ComboPooledDataSource datasource = null;
              for (int id = 1; id &lt;= 2; id++) {
                  datasource = getDatasource(id);
                  datasources.put(id, datasource);
              }
              return datasources;
          }
  
          private ComboPooledDataSource getDatasource(int id)
                  throws PropertyVetoException, ConfigurationException {
              ComboPooledDataSource datasource = new ComboPooledDataSource();
  
              // set the connection pool properties
              datasource.setJdbcUrl("jdbc:postgresql://localhost/hem" + id);
              datasource.setUser("hemant");
              datasource.setPassword("");
              datasource.setDriverClass("org.postgresql.Driver");
              datasource.setMaxPoolSize(30);
              datasource.setInitialPoolSize(10);
              datasource.setMinPoolSize(10);
  
              return datasource;
          }
      }
  </code></pre>
  
  <p>Also following setting is in application.properties so that automatic schema update is ON.</p>
  
  <pre><code># Hibernate ddl auto (create, create-drop, update)
  spring.jpa.hibernate.ddl-auto = update
  </code></pre>
  
  <p><strong>Issue: when I start the application, 
  the hbm2ddl schema update is performed only on the hem1 (defaultTargetDb) but not on other target databases</strong></p>
  
  <p>Following is the part of startup logs</p>
  
  <pre><code>[main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
  [main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000102: Fetching database metadata
  [main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000396: Updating schema
  [main] java.sql.DatabaseMetaData                : HHH000262: Table not found: users
  [main] java.sql.DatabaseMetaData                : HHH000262: Table not found: users
  [main] java.sql.DatabaseMetaData                : HHH000262: Table not found: users
  [main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000232: Schema update complete`enter code here`
  </code></pre>
  
  <p>THIS IS EXECUTED ONLY FOR 1 DB.</p>
  
  <p>**Later when I execute rest URL's say</p>
  
  <p>GET localhost:8080/users - results are fetched successfully for DB hem1 which is updated.</p>
  
  <p>But when GET 127.0.0.1:8080/users is accessed, since the schema is not updated/created, IT results in SQL Exception**</p>
  
  <p><strong>How can we ensure that "hbm2ddl schema update" is executed on all the target databases of a AbstractRoutingDataSource</strong></p>