title: Spring Data + JPA with multiple datasources but only one set of Repositories
body: <p>I've been researching this a bunch today and I'm starting to think that what I want to do may not be possible, so I am turning to you, o mighty Stackoverflow, for help.</p>
  
  <p>I'm building a RESTful services platform in Java, with Spring Data 3.1.2 + JPA as my persistence layer (as documented <a href="http://static.springsource.org/spring-data/data-jpa/docs/1.1.x/reference/html/" rel="noreferrer">here</a>). My data model objects are all implemented as interfaces that extend the Spring JpaRepository interface. I've got everything wired up and working nicely with a single datasource, as shown in this example (note that the datasource shown is Derby, but that's just for development purposes; in production, we'll be using Oracle):</p>
  
  <pre><code>&lt;jpa:repositories base-package="com.my.cool.package.repository"/&gt;
  
  &lt;bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
    &lt;property name="dataSource" ref="dataSource" /&gt;
    &lt;property name="loadTimeWeaver"&gt;
      &lt;bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /&gt;
    &lt;/property&gt;
    &lt;property name="packagesToScan" value="com.my.cool.package" /&gt;
    &lt;property name="jpaVendorAdapter" ref="jpaVendorAdapter" /&gt;
  &lt;/bean&gt;
  
  &lt;bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
    &lt;property name="entityManagerFactory" ref="emf" /&gt;
  &lt;/bean&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
    &lt;property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" /&gt;
    &lt;property name="url" value="jdbc:derby:derbyDB" /&gt;
    &lt;property name="username" value="dev" /&gt;
    &lt;property name="password" value="notARealPassword" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
    &lt;property name="databasePlatform" value="org.hibernate.dialect.DerbyTenSevenDialect" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The problem is that this application will need to connect to several (Oracle) databases. The credentials included with each incoming request will contain a field that tells the application which database to go to in order to fulfill that request. The schemas for each database are the same, so there's no need for separate repository interfaces for each database.</p>
  
  <p>After a fair amount of Googling, it's clear that this is a common scenario. To wit:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/9893274/multiple-databases-with-spring-data-jpa">multiple databases with Spring Data JPA</a></li>
  <li><a href="https://stackoverflow.com/questions/8703242/spring-hibernate-jpa-multiple-databases">Spring + Hibernate + JPA + multiple databases</a></li>
  <li><a href="https://stackoverflow.com/questions/5870911/how-to-setup-spring-data-jpa-with-multiple-datasources">how to setup spring data jpa with multiple datasources</a></li>
  </ul>
  
  <p>And here's a blog post by a (former?) Spring developer, which isn't actually relevant to the topic at hand, but someone brings it up in the comments, and the author responds with some info:</p>
  
  <p><a href="http://blog.springsource.org/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/#comment-198835" rel="noreferrer">http://blog.springsource.org/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/#comment-198835</a></p>
  
  <p>The theme that seems to be emerging is that the way to solve this problem is to define multiple EntityManagerFactories, and to wire each one to the appropriate repositories like so:</p>
  
  <pre><code>&lt;jpa:repositories base-package="com.my.cool.package.repository1" entity-manager-factory-ref="firstEntityManagerFactory" /&gt;
  &lt;jpa:repositories base-package="com.my.cool.package.repository2" entity-manager-factory-ref="secondEntityManagerFactory" /&gt;
  </code></pre>
  
  <p>However, as I've mentioned, I want to reuse my repository across all of the datasources, so this approach doesn't seem like it would work.</p>
  
  <p>I know that there's no way around having logic in my code that takes the relevant piece of information from the request, and uses it to determine which datasource (or EntityManagerFactory) to use. The part I'm struggling with is how to get a handle to that datasource/EntityManagerFactory and "inject" it into my repository objects. Any ideas?</p>