0

I have to create a Spring web application with the framework Spring MVC 4, Hibernate and MySQL. In this context, I need to connect to multiple MYSQL databases in the same application. Is it possible? If so, can you help me find the right way. Thank you.

Edit: I'm attempting to access two databases simultaneously from the same Java class in Spring/hibernate. They are identical in terms of structure. and they both reside in the same mySql instance. I'll call them "myDatabaseA" and "myDatabaseB".

What i need is to connect to many Mysql DataBases. (e.g : The application is dedicated to several clients and each of them must have a different BD but all are mysql database:DB-CLIENT1, DB-CLIENT2,DB-CLIENT3,... )

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Yes. Check how datasources are created and use them accordingly when setting up beans definition on top of them (adding the necessary datasource as dependency to be injected) – Alfabravo Feb 13 '17 at 16:20
  • thank you for your reply,but can you better explain to me. I'm a beginner. – habiba kessraoui Feb 13 '17 at 16:26
  • 1
    Possible duplicate of [How to use 2 or more databases with spring?](http://stackoverflow.com/questions/30362546/how-to-use-2-or-more-databases-with-spring) – Alfabravo Feb 13 '17 at 16:34

1 Answers1

0

For each database that you're connecting to, you need to setup javax.sql.DataSource and LocalContainerEntityManagerFactoryBean , JpaTransactionManager (if you're using JPA and Hibernate as your persistence provider) or LocalSessionFactoryBean and HibernateTransactionManager (if you're using Hibernate). And of course, your hibernate.cgf.xml for your hibernate related configurations

Shriram
  • 74
  • 7
  • Okay, then my comment above would work. As I said, separate javax.sql.DataSource, LocalContainerEntityManagerFactoryBean and JpaTransactionManager objects for each database that you're connecting to and you should be good to go. – Shriram Feb 15 '17 at 21:31
  • thanks a lot i will try your solution and i will see. – habiba kessraoui Feb 20 '17 at 08:19