Multiple datasources with same repositories and entities












1















I have a current requirement of allowing multiple data sources for the same repositories/entities in spring boot using jpa.



What I've managed to find was always related to multiple datasources, with multiple repositories/entites.



However, here I would like to have the same repositories and entities (ie same database structure) for multiple datasources.



And based on same property, programmatically activate a specific datasource.



I've tried the setup demo'ed here: https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources



And in both configurations use the same package in the factoryBean.setPackagesToScan() call. This does now work however, one datasource overrides the other.



My current test can be found here: https://github.com/nWidart/spring-data-multi-datasource/tree/master/src/main/java/com/example/multidatasources



It shows the 2 configuration files (client1 and client2) with a repository and entity. The controller has 2 endpoints for both data sources (not working).



Thanks!










share|improve this question



























    1















    I have a current requirement of allowing multiple data sources for the same repositories/entities in spring boot using jpa.



    What I've managed to find was always related to multiple datasources, with multiple repositories/entites.



    However, here I would like to have the same repositories and entities (ie same database structure) for multiple datasources.



    And based on same property, programmatically activate a specific datasource.



    I've tried the setup demo'ed here: https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources



    And in both configurations use the same package in the factoryBean.setPackagesToScan() call. This does now work however, one datasource overrides the other.



    My current test can be found here: https://github.com/nWidart/spring-data-multi-datasource/tree/master/src/main/java/com/example/multidatasources



    It shows the 2 configuration files (client1 and client2) with a repository and entity. The controller has 2 endpoints for both data sources (not working).



    Thanks!










    share|improve this question

























      1












      1








      1


      0






      I have a current requirement of allowing multiple data sources for the same repositories/entities in spring boot using jpa.



      What I've managed to find was always related to multiple datasources, with multiple repositories/entites.



      However, here I would like to have the same repositories and entities (ie same database structure) for multiple datasources.



      And based on same property, programmatically activate a specific datasource.



      I've tried the setup demo'ed here: https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources



      And in both configurations use the same package in the factoryBean.setPackagesToScan() call. This does now work however, one datasource overrides the other.



      My current test can be found here: https://github.com/nWidart/spring-data-multi-datasource/tree/master/src/main/java/com/example/multidatasources



      It shows the 2 configuration files (client1 and client2) with a repository and entity. The controller has 2 endpoints for both data sources (not working).



      Thanks!










      share|improve this question














      I have a current requirement of allowing multiple data sources for the same repositories/entities in spring boot using jpa.



      What I've managed to find was always related to multiple datasources, with multiple repositories/entites.



      However, here I would like to have the same repositories and entities (ie same database structure) for multiple datasources.



      And based on same property, programmatically activate a specific datasource.



      I've tried the setup demo'ed here: https://github.com/spring-projects/spring-data-examples/tree/master/jpa/multiple-datasources



      And in both configurations use the same package in the factoryBean.setPackagesToScan() call. This does now work however, one datasource overrides the other.



      My current test can be found here: https://github.com/nWidart/spring-data-multi-datasource/tree/master/src/main/java/com/example/multidatasources



      It shows the 2 configuration files (client1 and client2) with a repository and entity. The controller has 2 endpoints for both data sources (not working).



      Thanks!







      java spring spring-boot spring-data-jpa spring-data






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 19:55









      Nicolas WidartNicolas Widart

      3381417




      3381417
























          1 Answer
          1






          active

          oldest

          votes


















          1














          you can do like below :





          1. application.properties (as default) : as you see, you can switch between datasource files.
          enter image description here

          2. application-test.properties (sample)
          enter image description here

          I hope this can help you. :D






          share|improve this answer
























          • additinal for you. In big project, file application properties not save in project. It have infomation connect to database and not security. You should be use spring cloud config in here https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

            – phuchoangmai
            Nov 15 '18 at 3:10











          • Thank you. However, I'm looking to do this in one single data source. I came across this blog post which come close: spring.io/blog/2007/01/23/dynamic-datasource-routing

            – Nicolas Widart
            Nov 15 '18 at 21:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307851%2fmultiple-datasources-with-same-repositories-and-entities%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          you can do like below :





          1. application.properties (as default) : as you see, you can switch between datasource files.
          enter image description here

          2. application-test.properties (sample)
          enter image description here

          I hope this can help you. :D






          share|improve this answer
























          • additinal for you. In big project, file application properties not save in project. It have infomation connect to database and not security. You should be use spring cloud config in here https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

            – phuchoangmai
            Nov 15 '18 at 3:10











          • Thank you. However, I'm looking to do this in one single data source. I came across this blog post which come close: spring.io/blog/2007/01/23/dynamic-datasource-routing

            – Nicolas Widart
            Nov 15 '18 at 21:06
















          1














          you can do like below :





          1. application.properties (as default) : as you see, you can switch between datasource files.
          enter image description here

          2. application-test.properties (sample)
          enter image description here

          I hope this can help you. :D






          share|improve this answer
























          • additinal for you. In big project, file application properties not save in project. It have infomation connect to database and not security. You should be use spring cloud config in here https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

            – phuchoangmai
            Nov 15 '18 at 3:10











          • Thank you. However, I'm looking to do this in one single data source. I came across this blog post which come close: spring.io/blog/2007/01/23/dynamic-datasource-routing

            – Nicolas Widart
            Nov 15 '18 at 21:06














          1












          1








          1







          you can do like below :





          1. application.properties (as default) : as you see, you can switch between datasource files.
          enter image description here

          2. application-test.properties (sample)
          enter image description here

          I hope this can help you. :D






          share|improve this answer













          you can do like below :





          1. application.properties (as default) : as you see, you can switch between datasource files.
          enter image description here

          2. application-test.properties (sample)
          enter image description here

          I hope this can help you. :D







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 2:50









          sovannarith cheavsovannarith cheav

          894




          894













          • additinal for you. In big project, file application properties not save in project. It have infomation connect to database and not security. You should be use spring cloud config in here https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

            – phuchoangmai
            Nov 15 '18 at 3:10











          • Thank you. However, I'm looking to do this in one single data source. I came across this blog post which come close: spring.io/blog/2007/01/23/dynamic-datasource-routing

            – Nicolas Widart
            Nov 15 '18 at 21:06



















          • additinal for you. In big project, file application properties not save in project. It have infomation connect to database and not security. You should be use spring cloud config in here https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

            – phuchoangmai
            Nov 15 '18 at 3:10











          • Thank you. However, I'm looking to do this in one single data source. I came across this blog post which come close: spring.io/blog/2007/01/23/dynamic-datasource-routing

            – Nicolas Widart
            Nov 15 '18 at 21:06

















          additinal for you. In big project, file application properties not save in project. It have infomation connect to database and not security. You should be use spring cloud config in here https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

          – phuchoangmai
          Nov 15 '18 at 3:10





          additinal for you. In big project, file application properties not save in project. It have infomation connect to database and not security. You should be use spring cloud config in here https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html

          – phuchoangmai
          Nov 15 '18 at 3:10













          Thank you. However, I'm looking to do this in one single data source. I came across this blog post which come close: spring.io/blog/2007/01/23/dynamic-datasource-routing

          – Nicolas Widart
          Nov 15 '18 at 21:06





          Thank you. However, I'm looking to do this in one single data source. I came across this blog post which come close: spring.io/blog/2007/01/23/dynamic-datasource-routing

          – Nicolas Widart
          Nov 15 '18 at 21:06




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307851%2fmultiple-datasources-with-same-repositories-and-entities%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues