spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Improve documentation showing how to use two different databases

Open waffel opened this issue 10 years ago • 16 comments

It would be nice to get a working example, how to use and configure two databases in spring boot.

The documentation says, that it is only required to create a datasource configuration with two different datasources.

But this does not work complete. The example application can show how to:

  • define and use different data-${platform}.sql files to add data to two different test databases
  • use the datasources in repositories where we define simple an interface which extends from the CrudRepository interface
  • create a test case where data will be written into one datasource and read from another and then mix the result into a controller to deliver both data (via REST for example) to the client

I have not found any "complete" example for this. But it is mentioned in the documentation, that this is easy and supported by spring-boot .. so it would be nice to have such example.

waffel avatar Jul 10 '15 07:07 waffel

It would be interesting to see what is recommended for multiple databases. I have a multi-tenant solution that I have cobbled together and don't specifically trust. I know that I have memory leaks and presume that it is because I have deviated from the spring boot way. I have six jdbc bean pools configured and determine which one to inject into the service based on the restful call. This solution leaves me tightly bound to JTDS for a JDBC driver and, as mentioned above, I am trying to sort out memory leaks. image

jamesbehrens1 avatar Jul 10 '15 13:07 jamesbehrens1

Example is here https://github.com/eepstein/multids-demo

eepstein avatar Aug 05 '15 00:08 eepstein

There is also https://github.com/snicoll-demos/demo-multi-entity-managers that is meant to demonstrate the JPA/Spring Data use case with two datasources and Spring Boot.

snicoll avatar Jan 23 '17 12:01 snicoll

The documentation has also been updated as well.

snicoll avatar Mar 22 '18 19:03 snicoll

Can we reopen this issue? The example from https://github.com/snicoll-demos/demo-multi-entity-managers is really helpful for spring-boot 1.x

But the documentation for spring-boot 2.x does not tell us about how to separate (or setup) domain objects/packages to specific datasource (is we have more than one).

It also unclear how to setup special hibernate properties (for example) for each datasource and how a test can look (with a database auto creation and sql import like the "import.sql" feature worked for 1.x).

The mentioned example https://github.com/eepstein/multids-demo does no longer exists.

I would like to help to create a spring-boot 2.x example if somebody can help me to figure out how to do this. So I can start with a example repo (which I have already on my local) where at the end the tests are failing because of some missing peaces.

Thanks in advance

waffel avatar Apr 04 '18 15:04 waffel

@waffel I've re-opened the issue but it might take a while for us to get to this. Feel free to share the sample that you have created so far.

philwebb avatar Apr 04 '18 18:04 philwebb

thanks :+1:

I have managed to get it work with spring boot 2.0.1 .. I will start to create a demo repo and link it here .. there are some open topics which we can discuss on the demo repo or here.

waffel avatar Apr 11 '18 11:04 waffel

follow the comments from https://github.com/mybatis/spring-boot-starter/issues/78, I set it up successfully, but I run into the issue database initialize. when there is just one datasource it works well. but when there are two database, springboot does not pick up the schema-{platform}.sql. In my case it does not pick up schema-h2.sql. but if I rename schema-h2.sql to schema.sql. then it can load.

kcmvp avatar Apr 12 '18 08:04 kcmvp

sdk.datasource.jdbc-url=jdbc:h2:mem:sdk
sdk.datasource.username=sa
sdk.datasource.password=
sdk.datasource.driver-class-name=org.h2.Driver
sdk.datasource.initialize=true
sdk.datasource.platform=h2


app.datasource.driver-class-name=com.mysql.jdbc.Driver
app.datasource.initialize=true
app.datasource.platform=mysql
app.datasource.testWhileIdle = true
app.datasource.timeBetweenEvictionRunsMillis = 3600000
app.datasource.validationQuery = SELECT 1

and I have schema-h2.sql and schema-mysql.sql files in classpath

kcmvp avatar Apr 12 '18 08:04 kcmvp

@kcmvp please do not hijack this issue to ask questions. We use StackOverflow or Gitter for that.

snicoll avatar Apr 12 '18 08:04 snicoll

Is this issue still open?

rahul404 avatar Nov 15 '18 19:11 rahul404

@rahul404 there is an open label at the top of this page. Yes it is still open.

snicoll avatar Nov 15 '18 19:11 snicoll

Can i work on it?

rahul404 avatar Nov 15 '18 20:11 rahul404

@rahul404 In retrospects, I am not sure why it was flagged ideal for contribution. It looks like upgrading my sample to SB 2 would already be a good way forward but given the feedback we got so far, I think it should be more prominent than its current location.

snicoll avatar Nov 16 '18 12:11 snicoll

See also #14928

snicoll avatar Nov 16 '18 12:11 snicoll

We think that a guide would be a better way to describe how to proceed with this. We'll upgrade the current sample and move it to a guide.

snicoll avatar May 17 '19 13:05 snicoll