title: Spring boot JPA set custom datasource
body: <p>I have 2 datasources defined-</p>
  
  <pre><code>#datasource jndi setup
  database.datasources[1].jndi-name=jdbc/yyyy
  database.datasources[1].username=yyyy
  database.datasources[1].password=yyyyy
  database.datasources[1].url=jdbc:oracle:thin:@localhost:2222:webtst
  database.datasources[1].driverClassName=oracle.jdbc.OracleDriver
  database.datasources[1].factory=org.apache.tomcat.jdbc.pool.DataSourceFactory
  database.datasources[1].initialSize=1
  database.datasources[1].maxActive=5
  
  database.datasources[0].jndi-name=jdbc/xxx
  database.datasources[0].username=xxx
  database.datasources[0].password=xxxx
  database.datasources[0].url=jdbc:oracle:thin:@localhost:2222:webtst
  database.datasources[0].driverClassName=oracle.jdbc.OracleDriver
  database.datasources[0].factory=org.apache.tomcat.jdbc.pool.DataSourceFactory
  database.datasources[0].initialSize=1
  database.datasources[0].maxActive=15 
  </code></pre>
  
  <p>And 2 matching JPA repositories, how can I set one of them to use different ds(not the primary one) after I defined one of them to be @Primary : </p>
  
  <pre><code>@Bean
  @Primary
  DataSource dataSource() throws SQLException {
      OracleDataSource dataSource = new OracleDataSource();
      dataSource.setUser(xusername);
      dataSource.setPassword(xpassword);
      dataSource.setURL(xurl);
  
      dataSource.setImplicitCachingEnabled(true);
      dataSource.setFastConnectionFailoverEnabled(true);
      return dataSource;
  }
  
  
  
  @Bean
  DataSource propsDataSource() throws SQLException {
      OracleDataSource propsDataSource = new OracleDataSource();
      //propsDataSource.setDataSourceName(secJNDIName);
      propsDataSource.setUser(yUserName);
      propsDataSource.setPassword(ypropsPassword);
      propsDataSource.setURL(upropsUrl);
      propsDataSource.setImplicitCachingEnabled(true);
      propsDataSource.setFastConnectionFailoverEnabled(true);
      return propsDataSource;
  }
  </code></pre>
  
  <p>And a simple JpaRepository that I want it to use seconday data-source:</p>
  
  <pre><code>public interface AttributesRepo extends JpaRepository&lt;PRODUCTATTRIBUTE, PRODUCTATTRIBUTE_KEY&gt; {
  
  }
  </code></pre>