title: How to connect to multiple databases in Spring Boot JPA?
body: <p>I currently have one database connected and it is working. I would like to connect another (and eventually 2 more) databases. How do I do so? <strong>There should be a solution using only annotations and properties files.</strong> </p>
  
  <p>I also read this question, <a href="https://stackoverflow.com/questions/30362546/how-to-use-2-or-more-databases-with-spring">How to use 2 or more databases with spring?</a>, but I dont know how it works too well/ if it will apply. I'm not using a controller class and I dont know what that does. I'm also not sure how the config class they mention in the answer actually connects to the specific DO.</p>
  
  <p>This is my application.properties file: (marked out username and password but its there in my file)</p>
  
  <pre><code>hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
  hibernate.show_sql=true
  hibernate.format_sql=true
  hibernate.default_schema=dbo
  hibernate.packagesToScan=src.repositories.LMClientRepository.java
  
  spring.jpa.generate-ddl=true
  spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy
  spring.datasource.username=***
  spring.datasource.password=***
  spring.datasource.url=jdbc:sqlserver://schqvsqlaod:1433;database=dbMOBClientTemp;integratedSecurity=false;
  spring.datasource.testOnBorrow=true
  spring.datasource.validationQuery=SELECT 1
  spring.jpa.database=dbMOBClientTemp
  spring.jpa.show-sql=true
  spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
  spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
  </code></pre>
  
  <p>This is my application file:</p>
  
  <pre><code>package testApplication;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.orm.jpa.EntityScan;
  import org.springframework.cache.annotation.EnableCaching;
  import org.springframework.context.annotation.Bean;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  
  import fileRetrieval.InputFileParse;
  import lmDataObjects.LMClientDO;
  import lmDataObjects.LoadMethodDO;
  import repositories.LMClientRepository;
  import repositories.LoadMethodRepository;
  
  @SpringBootApplication
  @EnableJpaRepositories(basePackageClasses = LoadMethodRepository.class)
  @EntityScan(basePackageClasses = LoadMethodDO.class)
  @EnableCaching
  public class Application {
  
      private static final Logger log = LoggerFactory.getLogger(Application.class);
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  
      @Bean
      public CommandLineRunner demo(LoadMethodRepository lm_repo, LMClientRepository lmc_repo) {
          return (args) -&gt; {
              List&lt;LMClientDO&gt; lmlist = InputFileParse.getMultiGroupfile();
  
              List&lt;String&gt; uniqueMediaIds = new ArrayList(InputFileParse.getUniqueMediaIds());
  
              for (int i = 0; i &lt; InputFileParse.getUniqueMediaIds().size(); i ++){
                  lm_repo.save(new LoadMethodDO(uniqueMediaIds.get(i)));
              }
  
              for (int i = 0; i &lt; lmlist.size(); i++){
                  lmc_repo.save(new LMClientDO(lmlist.get(i).getClientId(), lmlist.get(i).getMediaId()));
              }
              //Here is where I would like to do stuff with data from the other database that I have not connected yet
  
          };
      }
  }
  </code></pre>
  
  <p>I also made a new properties file called application-MTS.properties and I put data for the new database in there. Still unsure of what to do with it.</p>
  
  <pre><code>spring.datasource.username=***
  spring.datasource.password=***
  spring.datasource.url=jdbc:sqlserver://SCHQVSQLCON2\VSPD:1433;database=dbMTS;integratedSecurity=false;
  </code></pre>