6

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!

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

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...

Comments

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.

1 Comment

Well, this does not set the connection at runtime as requested, does it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.