-1

Here i am explaining a problem where whole scenario comes under spring boot 2 version.

As you all know, when we having a spring boot application, the data source or db related things we will mention in application.propeties, apart from that when we launch or initialise a spring boot application, we don't configure or write a code for database explicitly.

Here my scenario is, I have a application which having large amount of code and where entities and repos are defined across different packages over the application. Now I have a case where I need to connect with another database to my application.(two database)

What i tried till now is to configure my two data source through appliaction.properties and configure data source class for two db. But here my problem is, since my entities are spread across different areas, i have mention root directory and consider all sub directories may contain entities. And also my application code has been written by multiple people.So where ever they us entityManager , i have to annotate those methods with @Transactional("transactionManagerName) and if readOnly i have to mention that also. This task is an absolute tedious one, since i have very big amount of code. And even though i mention all the annotation on the respective places, the possibility of errors coming future is depends upon my testing of code. So obviously a good amount time need to be spend on testing also, which is another heavy task for my current scenario.

So what i am looking for is, consider my current database which is only configured through application.properties as my default database and add on a new database , where i need to segregate entities to a specific package,(this is fine,since new one i can instruct people to do in a single package) and write configuration class only for new db which is totally fine. So the existing code stays as it is and won't be affected in any means. This is what i am looking for, of course there are solutions to connect multiple db,i have read so many articles. But my scenario is different from it. And one more catch here is i am still on spring boot 2 which is another drawback in my case. If someone who is very good at spring boot can help me out, it would be so thankful.

the things i tried till now 1 baeldung 2 stackoverflow 3 medium

6
  • The attached articles from Baeldung just seem fine regardless of SB2 or SB3. what issue are you facing? Commented Jul 6 at 4:57
  • I need to adjust my application to have multiple database where my first database and related code should not affected in any means, and same time i want configure another database. The thing here is, i need to add the second database like a profile, without interrupting the first one's flow
    – Ibu
    Commented Jul 6 at 5:01
  • concept of the profiling is different. if you want to include/exclude data sources based on profile like swict-on-db, switch-off-db then you will use profiling. However, in your case, you just need to define two @EnableJpaRepositories along with ` basePackages = "com.package1"` and that will do the trick. you don't have to touch existing code. new entities can be added to new package Commented Jul 6 at 5:19
  • Hi ish, here exisisting entities and repos are spread across different packages. I can't restructure that. I am looking something like Aspect oriented programming(AOP). I don't want define configure class for first database. I want it only for second database. First one need to be defined as default.
    – Ibu
    Commented Jul 6 at 7:54
  • You can specify multiple packages in @EnableJpaRepositories or just the parent package of existing entities. Create new package which is not inside that parent and scan it via another @EnableJpaRepositories Commented Jul 6 at 8:03

0

Browse other questions tagged or ask your own question.