This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Multiple transaction managers for multiple unrelated data sources

Aug 22nd, 2012, 12:40 AM

Hi,

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.