Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an example of how to configure multiple datasources and configuration consumption #7652

Closed
odrotbohm opened this issue Dec 15, 2016 · 7 comments
Assignees
Labels
type: documentation A documentation update
Milestone

Comments

@odrotbohm
Copy link
Member

It seems that there are a couple of tricks needed to get a manual setup of multiple DataSources working nicely including the externalization of properties. Ideally the sample would show:

  1. How to structure properties so that each DataSource has its own distinct configuration.
  2. Show how to declare @Bean methods to that they pick up the configuration from the declared property sets.
  3. Optionally (if possible and recommended), show how to share some of the properties declared (e.g. user and password) to avoid mistakes when configuration changes.
@snicoll snicoll self-assigned this Dec 15, 2016
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 15, 2016
@philwebb
Copy link
Member

@philwebb philwebb added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 15, 2016
@odrotbohm
Copy link
Member Author

That's a nice find actually. When skimmed the docs, I quickly found http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-configure-datasource scrolled through it, reached the Spring Data JPA specific bits and thought that was it. I didn't even think there could be anything further below. Linking to the HowTo from the reference section would be very helpful.

@philwebb philwebb added this to the 1.4.3 milestone Dec 16, 2016
@snicoll
Copy link
Member

snicoll commented Dec 19, 2016

I am looking at building the sample and some things are bugging me. I can see others in the community are confused by it.

Spring Boot advertizes that we can automatically detect the driver class name based on the URL and it will automatically provide an embedded URL if no url is provided. These are pretty interesting feature but we can't cleanly apply them when one wants to configure two datasources.

The main issue is that DataSourceProperties does it and it does more things: you can specify the jndi-name of the datasource (only used by the auto-configuration) and several attributes used to initialize the datasource.

The main goal of such arrangement is to keep the auto-completion in the IDE for the custom namespaces. With the current design, a bunch of keys would be exposed that have no effect. I've start to hack something in a branch and the end-result is quite concise and only exposes the key that actually do something in practice.

Still, we're not there yet. DataSourceAutoConfiguration exposes a DataSourceInitializer almost unconditionally and the latter makes a weird link between DatasourceProperties and DataSource. We should fix that (typically not do anything at all if a custom DataSource is provided).

All in all, I don't think that sample can go in 1.4. ping @philwebb

@snicoll snicoll modified the milestones: 1.5.0 M1, 1.4.3 Dec 19, 2016
snicoll added a commit that referenced this issue Dec 19, 2016
@snicoll snicoll added the for: team-attention An issue we'd like other members of the team to review label Dec 19, 2016
@cptwunderlich
Copy link

As an affected user, I'd like to comment on this discussion. I was just trying to implement a feature using two datasources.

I found the documentation mentioned above, but it has two issues:

  1. They use prefixes like 'datasource.primary', where the normal property is called spring.datasource.* (e.g., spring.datasource.initialize). So it looks like the resulting key would be 'spring.datasource.primary.', but looking at other examples, it would be 'datasource.primary.datasource.'?

  2. There is one section called 'Configure Two DataSources' and later 'Use Two EntityManagers'. The latter references code from the first one. But if you try out the code like that, you'll find out that something else is missing.

In fact, the best source I've found, was a stackoverflow posting referencing the docs and this example: https://github.com/snicoll-demos/demo-multi-entity-managers

The docs should also mention, that when you configure an additional DataSource/EntityManager, the auto-configuration won't work anymore. I added a secondary DataSource+EntityManager like in the example, but now I get a warning "No bean named 'entityManagerFactory' available" and a follow-up Exception

@snicoll
Copy link
Member

snicoll commented Dec 19, 2016

Thanks for the feedback.

I didn't get 1. If the prefix is datasource.primary, it is datasource.primary and none of the Spring Boot features apply since you're taking control over the configuration. It's a common principle in Spring Boot: if you configure things yourself, you are in control.

As of 2, I am actually keen on moving the sample from my repo to the main Spring Boot repo and reference it in the doc.

@philwebb philwebb modified the milestones: 1.5.0 RC1, 1.5.0 M1, 1.5.0 Dec 20, 2016
@cptwunderlich
Copy link

Ah, thanks. At first, I didn't even realize it was your example I found.

There seems to be duplicate issue: #3456

@snicoll snicoll added for-team-meeting and removed for: team-attention An issue we'd like other members of the team to review for: team-meeting labels Jan 4, 2017
@snicoll
Copy link
Member

snicoll commented Jan 10, 2017

Let's focus on showing how we can "reproduce" what Boot does for us (datasource properties, initializer, etc) and how an extra datasource can be added. #7942 will avoid a cast for the second datasource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

5 participants