0

I've read several examples ex1, ex2, ex3 for using several database on Spring Data (JPA), all of them suggest creating one Configuration file for each database, and three bean (EntityManager, DataSource and TransactionManager) for each entity. Is it the only way we can handle multiple database or there is easier way to do this ?

Community
  • 1
  • 1
Bheid
  • 306
  • 3
  • 11
  • When the data sources are known at design time and are not likely to change as the application grows, the easier option would be to configure them statically with individual `DataSource`s and `EntityManager`s. However, if the data sources are not known in advance or if there are other considerations that require the data source to be determined at runtime, there are other options. What exactly is your requirement and how do you want to use multiple data sources? – manish Mar 12 '17 at 09:44
  • @manish Data sources are known and are not likely to change, so could you provide an example of configure them statically? Is it required in this option to define EntityManager, DataSource and TransactionManager for every entity? – Bheid Mar 13 '17 at 03:52
  • Please see the [official example](https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources) from the Spring Data team. – manish Mar 13 '17 at 13:27
  • @manish , I've seen this example before. it's exactly implement what I ask to not implement, did you notice the main requirement of question? (without entityManager, DataSource and TransactionManager) – Bheid Mar 14 '17 at 05:37
  • The JPA specification requires entities to be loaded through an `EntityManager` and all entities associated with an `EntityManager` form its persistence context. The spec allows only one `DataSource` per persistence context. Since a `DataSource` represents a single relational database (or schema), multiple databases automatically implies multiple `DataSource`s and therefore multiple persistence contexts and `EntityManager`s. Therefore, there is no way around defining multiple `EntityManager`s and `DataSource`s if you want to work with multiple databases known at design time. – manish Mar 14 '17 at 06:15

0 Answers0