Can multiple data sources share models? - spring

Referring to this approach for implementing multiple datasources: https://www.baeldung.com/spring-data-jpa-multiple-databases
However for my case I am connecting to multiple datasources that are the same structure but different environments. (I need to connect to dev/qat/stg... environment databases for the same model).
Is there a way to point the dev<modelName>Repository / qat<modelName>Repository to the associated PlatformTransactionManager and also have those repositories share the same model so when changes are made I dont have to make the same change in multiple places?
I also need liquibase to work but only for the current spring.profiles.active environment if that makes any difference.

Related

An easy way to work with multiple same conception datasources in spring boot

I have two databases with only two deficiencies: URL and data, the tables are the same, even some tables's contents are the same.
I have managed two have each one as a separate datasource following this tutorial : https://www.baeldung.com/spring-data-jpa-multiple-databases .
However, code maintainability is hard, all made modifications on an entity, repository or even entity id class in one datasource have to be duplicated to the other one each time.
I am looking for configurations or practices or even ideas to make the code maintainability easier.
using spring 2.7.4

Multiple databases (Postgresql in RDS) but same spring repository and entity

I have a use case where I need to create exact same postgresql database in two different regions. Everything is same in these two databases i.e same schema and same tables and same data.
I have a use to achieve distributed transaction. So if a request land in region-a and write to region-a database to let's say Person table, then exact same record must be either written in Person table in both these database or if there is any error, write attempt should be rolled back.
I am trying to figure out if I can attach two different datasources with same Person Entity and CRUD repository in spring so the respoistory.save() method can write to Person table in both the databases.
So far, I have come across AbstractRoutingDataSource but that is for achieving multi tenancy in the databases. Other solutions are found are slightly different where use case is to write different records in different database (mostly sharding based on various data points).
Does spring provide any out of the box solution so I can achieve transactional write to same table in two different databases.
Does spring provide any out of the box solution so I can achieve transactional write to same table in two different databases.
Depends on your definition of "out of the box" - it doesn't itself implement distributed transactions, but does have support for using libraries that do. It is however relatively complicated to get everything working correctly, and requires additional components to be carefully configured in your runtime environment.
Spring Boot 2.x documentation on distributed transactions is here: https://docs.spring.io/spring-boot/docs/2.7.x/reference/htmlsingle/#io.jta
The Spring Boot 3.x documentation is here: https://docs.spring.io/spring-boot/docs/current/reference/html/io.html#io.jta but it's also worth noting that for 3.x, the Spring Boot team have changed direction and decided that integrated support should be provided by the relevant JTA provider (cf. https://github.com/spring-projects/spring-boot/issues/28589 ), and so there's projects like https://github.com/snowdrop/narayana-spring-boot

Atomikos - Working with two databases and single repository

I'm playing around with Atomikos. Using this as a starting point: https://www.fabiomaffioletti.me/blog/2014/04/15/distributed-transactions-multiple-databases-spring-boot-spring-data-jpa-atomikos/
In my use case, I'm going to have essentially two physically separated databases that are going to be identical.
This requirement is based on GDPR and basically, we will have databases in different regions.
Some of the tables need to be in sync and one of the ideas is to use Atomikos (2-phase commit ) to implement distributed transactions.
What I'm trying to implement is to use one single spring repository that is going to persist data into two physically divided databases.
All examples provided by atomikos are dealing with two databases but with different tables using different repositories.
Is it possible to configure atomikos on the way that I'm using a single repository that is going to persist data in both data sources?
In the ideal case, I'm looking for the possibility to mark service method with a specific annotation and specific transaction manager and only in that case to force that specific method or better to say invoked repository methods in that service methods ( save, saveAll methods and so on ) to persist data in both databases. When there is no specific annotation on a service method to persist only in a single database to not perform distributed transaction.

How to call database on multiple environments using Springboot JPA

I am new to Springboot and trying to build a small rest-service. We have a DB deployed on different environments (e.g. DEV, TEST). The rest-service will make a call to the appropriate database based on the received query param (e.g. ?env=TEST). The schemas of the deployed database are the same, the difference is only in connection string. I have some questions related to this task.
I read a few articles how to work with multiple databases using Spring JPA (for example this one: https://www.baeldung.com/spring-data-jpa-multiple-databases). It did work, but in the given example they get different entites from different databases using different queries, in my case the entity and the query is the same, but I still have to duplicate repositories, transactionManagers, entityManagers etc because of different datasources. And this is just two environments and I have more of them.
I have another thought that I might need to recreate the repository each time I process a request (to make the repository non-singleton). I am not sure if it is a good practice.
Maybe it worth to use JDBCTemplate instead of Spring JPA in this case?
Could you please suggest something how to approach such a task?

Spring Environment profiles and server properties

I have a requirement to load properties for different environments like DEV, QA and I have different properties file for each environment. So I solved this by setting environment property in server and accessing this value to load respective property files. When googled I found that Spring Environment Profiles provides the solution for similar scenarios. However, even here I have to set active-profile variable in server as environment variable.
What are the benefits of using Spring Environment Profiles over my native approach?
Profile lets you override anything in the Spring Context, properties, beans etc, from environment to environment, customer to customer. It is a easy and clean way to have custom implementations at any level of your beans.
For example, Lets assume your are building a product which read data from a relational database, you can develop DAO layer with profile="default". Then if another customer of yours or you yourself want to provide NoSQL support, you can develop another DAO layer with profile="nosql". This will make sure you can same product on both support based on profile. Easy and clean.
I am working on a project which have profile="local" which will help you bring application locally with out any database dependency (kind of mock mode). You can think of million other applications like to make use of Profile concept.

Resources