Skip to content

Conversation

@tinexw
Copy link
Contributor

@tinexw tinexw commented Oct 12, 2017

Fixes gh-8690

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 12, 2017
@tinexw tinexw force-pushed the 8690 branch 3 times, most recently from 7d292ed to 2e3b1ff Compare October 12, 2017 20:14
Copy link
Member

@snicoll snicoll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, I've added a few suggestions.

return this.url;
}

logger.debug("No url defined, looking for embedded database.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think adding a logging statement here is part of what is discussed in the issue you've referenced.

Copy link
Contributor Author

@tinexw tinexw Oct 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea here was to give more insight into what's happening in the case described in the issue. As far as I understood it is hard to find out why there is this error because of the "magic" happening with with embedded database (i.e. no url means: try to connect to an embedded database)

But the original issue was about something different anyway, I was trying to do the

Let's fix the error message though

So I can remove the logging statement if you want.

@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties
implements BeanClassLoaderAware, EnvironmentAware, InitializingBean {
implements BeanClassLoaderAware, EnvironmentAware, InitializingBean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please fix this formatting change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

public String getUrl(String databaseName) {
Assert.hasText(databaseName, "DatabaseName must not be null.");
return String.format(this.url, databaseName);
return this.url != null ? String.format(this.url, databaseName) : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That wasn't the intention as raised on #8690 - The idea is to avoid a NPE by an abuse of DataSourceProperties (as explained in the comments). I'd rather have an IllegalStateException here that states the url should not be null or something.

Thoughts?

Copy link
Contributor Author

@tinexw tinexw Oct 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the result for the user would basically be the same.
With this implementation they get an DataSourceBeanCreationException with the message Cannot determine embedded database url
If I change it to your proposal they get an IllegalStateExceptionwith a message that states the url should not be null or something.

I would prefer to not add the null check here because one of the enum constants is NONE(null, null, null), so calling the getUrl method on this enum constant would result in an exception which is not so nice because if we wanted to enforce the not null policy it should not be possible to even create objects that violate this policy imho.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference is that you give a perfectly valid exception message for a use case that isn't. I don't know if you've picked up that issue or if you faced it yourself but the issue as described doesn't result from a legit use case. I think we should then treat it as such.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it is a legit use case to define DataSourceProperties without a url - at least in the current implementation. Because then this fallback to the embedded database takes place (in my opinion it would be better to not have this magic at all but it’s there already so I don’t think it can/should be changed and is definitely not in scope of this issue.)
The error case from the issue is that no url is defined and no embedded database is available.
I just think the exception should be thrown from the DataSourceProperties and not from the EmbeddedDatabaseConnection because if you don’t even add an embedded database, why should you get an error that originates in an EmbeddedDatabaseConnection class?
But maybe I misunderstand this completely...?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But maybe I misunderstand this completely...?

Or I do. Let me run that thing concretely, thanks for the feedback.

@snicoll snicoll self-assigned this Oct 20, 2017
@snicoll snicoll changed the title Fix NPE if no database url is given Fix NPE if no database url is given and no embedded database is available Oct 25, 2017
@snicoll snicoll added priority: normal type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 25, 2017
@snicoll snicoll added this to the 1.5.9 milestone Oct 25, 2017
@snicoll snicoll closed this in 81e33dc Oct 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants