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

DataSourceBeanCreationFailureAnalyzer should suggest adding 'spring.datasource.url' property

Open SebastianDietrich opened this issue 2 years ago • 1 comments

I've generated a spring boot project using https://start.spring.io/ with java, maven, spring boot 2.7.7. I did not add/change anything. When adding PostgreSQL and spring-data-jpa (or spring-data-jdbc) I get the following when running mvn test:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

...

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

When adding @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }) to src/main/java/com/example/demo/DemoApplication.java the test runs fine

The expected behavior would be that a newly generated spring boot project tests and runs out-of-the-box (even if some further configurations are missing). Or at least the above infos "Consider the following..." should mention that one needs to set the driver

SebastianDietrich avatar Jan 15 '23 08:01 SebastianDietrich

The expected behavior would be that a newly generated spring boot project tests and runs out-of-the-box (even if some further configurations are missing).

That won't work. The @SpringBootTest starts your application as your main method would do, and as you included the PostgreSQL driver, tries to bootstrap a datasource.

If you include an embedded database dependency (h2, HSQL or derby) then the tests would automatically reconfigure to use that.

Or at least the above infos "Consider the following..." should mention that one needs to set the driver

That i can agree to. The "Consider the following:" section could include a hint to spring.datasource.url.

mhalbritter avatar Jan 16 '23 09:01 mhalbritter

i used mySQL and i added the annotation that has been suggested up and still the system generating the same issue. i added question on stackoverflow but i did not get any answer

https://stackoverflow.com/questions/75520481/springboot-v4-4-17-2-mysql-failed-to-determine-a-suitable-driver-class

mhussein76 avatar Feb 22 '23 02:02 mhussein76

@mhalbritter I am also facing the same issue for a while now check lots of third party website including stack overflow, all I can say is the problem seems to be very inconsistent.

I have the code where I anyone could recreate the issue, should I send it.

rabbanimdrb avatar Apr 18 '23 18:04 rabbanimdrb

@rabbanimdrb The problem described here is very consistent, and the steps to reproduce it have been provided. We've categorized this as a bug, as the Actions from the failure analyzer should include setting the spring.datasource.url property.

the problem seems to be very inconsistent

What makes you say that the problem is inconsistent?

scottfrederick avatar Apr 18 '23 19:04 scottfrederick

@rabbanimdrb The problem described here is very consistent, and the steps to reproduce it have been provided. We've categorized this as a bug, as the Actions from the failure analyzer should include setting the spring.datasource.url property.

the problem seems to be very inconsistent

What makes you say that the problem is inconsistent?

What I meant to say is, the problem I encounter is not consistent, as I follow the exact same steps each time, yet it occurs occasionally, while there are instances when it does not appear at all.

English is not my first language, so please forgive any issues with my language proficiency.

rabbanimdrb avatar Apr 19 '23 19:04 rabbanimdrb

@rabbanimdrb if you think you found a bug, please consider opening a new issue where you describe your problem and please also attach a sample with which we can reproduce the problem. Thanks!

mhalbritter avatar Apr 28 '23 07:04 mhalbritter

That i can agree to. The "Consider the following:" section could include a hint to spring.datasource.url

It used to be this way and we removed it. This code path can be reused in custom code (typically to create multiple datasources) and hardcoding "our" prefix is going to break that.

snicoll avatar Apr 30 '23 16:04 snicoll

Ah, thanks for the context @snicoll. I'll close this issue then.

mhalbritter avatar May 02 '23 06:05 mhalbritter

https://github.com/spring-projects/spring-boot/issues/12947 is the issue where the specific mention of spring.datasource.url was removed.

I wonder if there's some middle ground to be found here. Could we mention the url property (without assuming a prefix) in the "Consider the following" section? I also wonder if we should use "property" rather than "attribute" in "'url' attribute is not specified".

wilkinsona avatar May 02 '23 07:05 wilkinsona