Announcement Announcement Module
No announcement yet.
Multiple transaction managers for multiple unrelated data sources Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple transaction managers for multiple unrelated data sources


    I am using Spring Data JPA's Repositories to query two distinct data sources from within my application. Note that these sources are unrelated and do NOT require any sort of coordinated/JTA transactionality (in fact, one data source is being used purely for read-only queries). All of the concrete CrudRepository method implementations are, however, marked @Transactional (the non-read-only ones, at least).

    I am also using annotation configuration, so I have two completely unrelated sets of methods inside two completely unrelated @Configuration classes to define my DataSources (via JNDI), my PersistenceUnitManagers, my LocalContainerEntityManagerFactories, and my PlatformTransactionManagers. I have two each of these - one set for each distinct data source.

    Unfortunately, as soon as I call any of my Repository methods, I get the following error:

    No unique bean of type [org.springframework.transaction.PlatformTransactio nManager] is defined: expected single bean but found 2
    Ok, so that's obvious - I know I have two PlatformTransactionManagers. The question is, why is that a problem? All I want to do is query two unrelated data sources in completely unrelated transactions. Why can't I have two transaction managers?

    For each Repository class I have explicitly set the relevant entity manager (via JpaRepositoryFactoryBean.setEntityManager(...)) and the transaction manager (via JpaRepositoryFactoryBean.setTransactionManager(... ), so I would have assumed that this would be sufficient to allow everything to just magically work.

    Note that all of this works 100% fine if I remove everything relating to the second data source.

    I have also tried specifying @Primary on one of the transaction managers, but this made no difference.

    Last edited by sjlloyd; Aug 21st, 2012, 11:47 PM. Reason: Added note about trying use of @Primary