title: Adding a second datasource - SpringBoot RepositoryRestService PersistenceConfig
body: <p>I'm trying to find the best approach to adding a second datasource to our application. The main purpose is to expose CRUD ops against the db via rest, &amp; need to bounce against a 2nd db for authentication and role management. We are not using XML configs.</p>
  
  <p>Is there a way to simply add a second datasource bean in the existing PersistenceConfig.java file, or do we need to replicate the whole config class for the second db instance?</p>
  
  <p>The application:</p>
  
  <p>package foo;</p>
  
  <pre><code>import foo.config.PersistenceConfig;
  import foo.config.RepositoryRestConfig;
  import foo.config.WebConfig;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Import;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  
  @Configuration
  @ComponentScan
  @EnableJpaRepositories
  @Import({PersistenceConfig.class, WebConfig.class, RepositoryRestConfig.class})
  @EnableAutoConfiguration
  public class Application {
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  
  }
  </code></pre>
  
  <p>The Repo:</p>
  
  <pre><code>package foo.repository;
  
  import foo.Widget;
  import org.springframework.data.repository.CrudRepository;
  import org.springframework.data.repository.query.Param;
  import org.springframework.data.rest.core.annotation.RepositoryRestResource;
  
  import java.util.List;
  
  @RepositoryRestResource(collectionResourceRel = "widgets", path = "widgets")
  public interface WidgetsRepository extends CrudRepository&lt;Widget, Long&gt; {
      List&lt;Widget&gt; findByWidgetId(@Param("widgetid") long widgetId);
  }   
  </code></pre>
  
  <p>The persistence config :</p>
  
  <pre><code>package foo.config;
  
  import org.apache.commons.dbcp2.BasicDataSource;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Import;
  import org.springframework.context.annotation.Primary;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
  import org.springframework.orm.jpa.JpaDialect;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.Database;
  import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  import org.springframework.transaction.PlatformTransactionManager;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  import javax.persistence.PersistenceContext;
  import javax.sql.DataSource;
  
  
  @Configuration
  @Import(RepositoryRestMvcConfiguration.class)
  @EnableJpaRepositories
  @EnableTransactionManagement
  
  public class PersistenceConfig {
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
          final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          vendorAdapter.setDatabase(Database.SQL_SERVER);
          vendorAdapter.setShowSql(true);
  
          final LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
          factory.setJpaVendorAdapter(vendorAdapter);
          factory.setPackagesToScan("foo.model");
          factory.setDataSource(dataSource());
          return factory;
      }
  
      @Bean(destroyMethod = "close")
      public DataSource dataSource() {
          BasicDataSource dataSource = new BasicDataSource();
          dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          dataSource.setUrl("jdbc:sqlserver://127.0.0.1:1433;databaseName=fooDB");
          dataSource.setUsername("sa");
          dataSource.setPassword("*******");
          dataSource.setTestOnBorrow(true);
          dataSource.setTestOnReturn(true);
          dataSource.setTestWhileIdle(true);
          dataSource.setTimeBetweenEvictionRunsMillis(1800000L);
          dataSource.setNumTestsPerEvictionRun(3);
          dataSource.setMinEvictableIdleTimeMillis(1800000L);
          dataSource.setValidationQuery("SELECT 1");
          return dataSource;
      }
  
  
      @Bean
      public JpaDialect jpaDialect() {
          return new HibernateJpaDialect();
      }
  
      @Bean
      public PlatformTransactionManager transactionManager() {
          JpaTransactionManager txManager = new JpaTransactionManager();
          txManager.setEntityManagerFactory(entityManagerFactory().getObject());
          return txManager;
      }
  
  }
  </code></pre>
  
  <p>Thank you for your assistance...</p>