4

In Spring MVC, How to connect to two databases (Mysql database and MongoDB) in the same

Faycel Gsouma
  • 65
  • 1
  • 1
  • 4

1 Answers1

2

Your problem is not related to mvc module, but is more related to data access layer.

Making it simple, you need to configure 2 different datasources, with respective entity manager and transaction manager.

Then in your dao classes you can inject the needed entity manager.

@PersistenceContext(unitName="entityManager1")
private EntityManager entityManager1;

@PersistenceContext(unitName="entityManager2")
private EntityManager entityManager2;

Searching on google you can best find your needs, depending on which module you are using in data layer. One suggestion is to take a look at spring data module, it abstracts a lot of things.

Here an example of an xml configuration of multiple datasources with spring data: https://github.com/spring-projects/spring-data-jpa/blob/master/src/test/resources/multiple-entity-manager-integration-context.xml

gipinani
  • 14,038
  • 12
  • 56
  • 85