5

I have a spring-boot application with one database. Now I would like to integrate the possibility to copy some data from this database to another database. The connection to the second database should be done during runtime. I insert in my frontend the second database connection and start the copy from the first database to the second. The two databases have the same tables and I would like to use the same repositories. Is it possible to do that? If yes, do you know some code example or documentation? I'm trying to find the correct approach to do that.

Thank you!

Mah
  • 93
  • 1
  • 9

3 Answers3

3

Alternative solution could be to use AbstractRoutingDatasource: https://www.baeldung.com/spring-abstract-routing-data-source

Mah
  • 93
  • 1
  • 9
1

I would like to write code for others on the same search:

_context.Database.CloseConnection();
_context.Database.SetConnectionString("NewConnectionString");
_context.Database.OpenConnection();

this will connect you whatever DB you want to connect...

SoftWar
  • 63
  • 1
  • 7
0

This answer explains how to configure two data sources for a single Spring Boot application.

Also this tutorial explains the same concept with a detailed example.

The copying procedure could be done by fetching all the objects from each table on the main db, deleting id values and saving them to the second db.

gsan
  • 549
  • 1
  • 4
  • 14