title: How to add new Datasource at runtime Spring Boot
body: <p>I have this, but I don't find any possible solution. All the answers I found was about configuring two or more Datasource or Multitenant Dabatase, but It is not what I need. <br/>
  I have to do this:</p>
  
  <ol>
  <li>Config the 1st Datasource from application.properties. This is the primary Database configuration</li>
  </ol>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(
          entityManagerFactoryRef = "primaryEntityManagerFactory",
          basePackages = "com.example.primary")
  public class SmartConnectConfig {
  
      @Primary
      @Bean(name = "primaryDatasource")
      @ConfigurationProperties(prefix = "spring.datasource")
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setUrl(env.getProperty("spring.datasource.url"));
          dataSource.setUsername(env.getProperty("spring.datasource.username"));
          dataSource.setPassword(env.getProperty("spring.datasource.password"));
          return dataSource;
      }
  
      @Primary
      @Bean(name = "primaryEntityManagerFactory")
      public LocalContainerEntityManagerFactoryBean entityManagerFactory(
              EntityManagerFactoryBuilder builder,
              @Qualifier("primaryDatasource") DataSource dataSource) {
          return builder
                  .dataSource(dataSource)
                  .packages("com.example.primary")
                  .persistenceUnit("primary")
                  .build();
      }
  
      @Primary
      @Bean(name = "transactionManager")
      public PlatformTransactionManager transactionManager(
              @Qualifier("primaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
          return new JpaTransactionManager(entityManagerFactory);
      }
  </code></pre>
  
  <ol start="2">
  <li>When the user connected in the system depending on the organization make a query an get all the properties or the organization Database saved in the 1st Database for getting config of 2nd Datasource (url, username, password, driverClassName). This is <b>VERY IMPORTANT</b> because depending on user login I need to create different Datasource depending on the organization.</li>
  <li>Add to spring to manage some repository with the 2nd Datasource<br/>
  Note: 1st and 2nd repositories are in the different package for Spring can make a scan of those repositories, the second package is "com.example.second".</li>
  </ol>
  
  <p>Someone can give some suggestions.<br/>
  Thanks</p>