see gh-19192
Btw, I found an issue in DataSourceBuilder, the builder does not support SimpleDriverDataSource without alias aliases.addAliases("driver-class-name", "driver-class")
Here is a test to reproduce:
@Test
void simpleDriverDataSource() {
this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").type(SimpleDriverDataSource.class).build();
assertThat(this.dataSource).isInstanceOf(SimpleDriverDataSource.class);
assertThat(this.dataSource).extracting("driver").isNotNull();
}
`
UPDATE
My first approach was incorrect. Sorry for the inconvenience.
Comment From: snicoll
Thanks for the PR @nosan.
I found an issue in DataSourceBuilder, the builder does not support SimpleDriverDataSource
It's not meant to at the moment. The supported connection pools are commons-dbcp2, tomcat and hikari (default).
Comment From: nosan
Thanks for the feedback, @snicoll
It's not meant to at the moment. The supported connection pools are commons-dbcp2, tomcat and hikari (default).
Yes, I saw that when I was working on this PR.
Consider this test in DataSourceAutoConfigurationTests:
@Test
void explicitTypeSupportedDataSource() {
this.contextRunner
.withPropertyValues("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
"spring.datasource.type:" + SimpleDriverDataSource.class.getName())
.run(this::containsOnlySimpleDriverDataSource);
}
Actually, this test just checks that SimpleDriverDataSource will be created, and actually, it will, but still, if you try to getConnection from this DataSource you will get an exception:
java.lang.IllegalArgumentException: Driver must not be null
at org.springframework.util.Assert.notNull(Assert.java:198)
at org.springframework.jdbc.datasource.SimpleDriverDataSource.getConnectionFromDriver(SimpleDriverDataSource.java:139)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:205)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:169)
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationTests.containsOnlySimpleDriverDataSource(DataSourceAutoConfigurationTests.java:172)
This happens because DataSourceBuilder does not add alias:
```
aliases.addAliases("driver-class-name", "driver-class");
**P.S.** I think you are right and this is not related to the initial issue.
**Comment From: snicoll**
I was looking at this PR and I am not keen to bring support for `SimpleDriverDataSource` back in `2.1.x`. I'd rather let it fail which is what would happen if there wasn't an embedded driver on the claspath in the first place.
With a polish that doesn't bring support for `SimpleDriverDataSource` it would fail as follow (rather than creating an embedded database if one is available):
Caused by: java.lang.IllegalStateException: No supported DataSource type found at org.springframework.boot.jdbc.DataSourceBuilder.getType(DataSourceBuilder.java:140) at org.springframework.boot.jdbc.DataSourceBuilder.build(DataSourceBuilder.java:72) at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Generic.dataSource(DataSourceConfiguration.java:125) ```
I suggest to improve that so that our failure analyzer catch that and provide a dedicated error message.
We can then add support for SimpleDriverDataSource in 2.3.x as a fallback for this scenario. Adding such support is required for r2dbc anyway.
Comment From: filiphr
@snicoll the problem is not about not having an embedded driver on the classpath, but rather having a wrong or missing a connection pool. Which leads to using a wrong database even though you have configured your datasource url.
Originally I opened issue #19192 which was closed in favour of this PR.
Comment From: snicoll
Thank you, I am aware of the problem and I read the related issue already.
Comment From: snicoll
@nosan thank you for your effort on this but I am not keen to bring SimpleDriverDataSource in 2.1.x. Looking at the issue, avoiding the embedded configuration to kick in if an url is set sounds like a safer and more isolated change to me. I've just done that and I've reopened the related issue. Thanks again.