0

I'm trying to configure a Spring Boot application with two datasources using same repositories and models, but when spring boot start says that repositories are already defined and can't create another on the bean container. That's true I mean.. I accept the error..but I would like to know if there is any way to do this. Below an idea of what I would like to do

  • Datasoruce(H2) for local deploy ----> com.myapp.repositories
  • Datasource(SQLServer) for remote deploy ----> com.myapp.repositories

My problem is that even if I'm doing the deploy on my local machine ..I need my remote datasource to do some operation.. In other world both local and remote Datasource connections should be up during local deploy.

I found actually three solution:

  1. The first is @Profile the @Configuration one for "prod" one for "dev"..but in my case don't work.
  2. Second one is to put different repositories package for each configuration. This work but I need to move all repositories when I go live.
  3. Third solution is to do always two configuration and don't specify the basePackages on the remote Datasource. This work but I need to change this parameter when I go live.

Is there any other solution to do this? Thank you for help.

LucaT
  • 333
  • 5
  • 13
  • 1
    Possible duplicate of [Spring Boot Configure and Use Two DataSources](https://stackoverflow.com/questions/30337582/spring-boot-configure-and-use-two-datasources) – Rafał Sokalski Apr 19 '19 at 06:44
  • From a quick Google search, this seems to be possible: https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7 – Tim Biegeleisen Apr 19 '19 at 06:44
  • On medium.com solution they split the package repositories..one is com.foobar.bar.repo and the other one com.foobar.foo.repo ..in this case of course it work but not if repositories are in the same packages. – LucaT Apr 19 '19 at 06:58
  • You said: `In other world both local and remote Datasource connections should be up during local deploy` Can you explain why? –  Apr 19 '19 at 07:08
  • The application I'm doing need a remote authentication even if is deploy locally. For this reason the remote db connection should be up! In other world the app run on a db the authentication into another db. This problem could be solved by creating an additional software layer for authentication such as an api.. In this way only one db conn..and an api for the authentication...but at the moment is not possible. – LucaT Apr 19 '19 at 07:14
  • In your place, I would create a special datasource for the authentication purpose only, connected to SQL Server, with dedicated models and repositories, only for tables involved in authentication. Then, you'll be able to use two profiles: "prod" and "dev" and keep the same repositories and model for both. –  Apr 19 '19 at 07:23
  • Yes I understand your point but I'll try to explain in a better way. I have two DB one for production SQLServer and one for dev H2. In my project I include a library via Maven that need the SQLserver connection to be up for authentication. H2 instead is used only for local dev but when I go live I need to use all the models and repositories already used on H2. In other world all the code I wrote for the H2 logic should be used at the end for the SqlServer DB. In a dev stage remote connection should only serve the auth library but on prod should also perform what h2 do. – LucaT Apr 19 '19 at 07:32
  • 1. Maintain profiles to know at runtime if you are on dev or prod 2. Use Hiberante Multi-tenancy database strategy and based on your profile - switch the database to the prod for authentication and to the dev DB for the rest of DB calls. – Adil Khalil Apr 19 '19 at 07:53
  • That perfectly doable following what I said: separated authentication module with a special datasource, always up, and two datasources: one for H2 the other for SQL Server, one for each profile, both wired to the same repositories. –  Apr 19 '19 at 08:48
  • I've seen the multi-tenancy..and looks quite good... In any case when I configure the two datasource with the @EnableJpaRepositories with the same repository package I receive this error **The bean 'testRepository', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.**. Could you show me a real example with code? – LucaT Apr 19 '19 at 09:33

0 Answers0