spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.58k stars 40.55k forks source link

Improve documentation showing how to use two different databases #3456

Open waffel opened 9 years ago

waffel commented 9 years ago

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:

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.

jamesbehrens1 commented 9 years ago

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

eepstein commented 9 years ago

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

snicoll commented 7 years ago

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 commented 6 years ago

The documentation has also been updated as well.

waffel commented 6 years ago

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

philwebb commented 6 years ago

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

waffel commented 6 years ago

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.

kcmvp commented 6 years ago

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 commented 6 years ago
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

snicoll commented 6 years ago

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

rahul404 commented 5 years ago

Is this issue still open?

snicoll commented 5 years ago

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

rahul404 commented 5 years ago

Can i work on it?

snicoll commented 5 years ago

@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 commented 5 years ago

See also #14928

snicoll commented 5 years ago

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.