-
Notifications
You must be signed in to change notification settings - Fork 40.8k
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
DatabaseDriver swallows real exception #34728
Comments
The only place where "Unable to detect database type" appears is Line 132 in c0f59a1
|
Thanks for the report. Unfortunately, I don't think we can change I can think of two alternatives that I'd like to discuss with the team: OneWe could introduce a new method on /**
* Find a {@link DatabaseDriver} for the given {@code DataSource}.
* @param dataSource data source to inspect
* @param failureMapping failure to DatabaseDriver mapping to apply when a failure
* occurs
* @return the database driver, {@link #UNKNOWN} if not found, or the result of
* applying the failure mapping
* @since 2.7.10
*/
public static DatabaseDriver fromDataSource(DataSource dataSource,
Function<Exception, DatabaseDriver> failureMapping) {
try {
String productName = JdbcUtils.commonDatabaseName(
JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getDatabaseProductName));
return DatabaseDriver.fromProductName(productName);
}
catch (Exception ex) {
return failureMapping.apply(ex);
}
} Calling code can then return TwoAlternatively, we could update private DatabaseDriver getDatabaseDriver(DataSource dataSource) {
try {
String productName = JdbcUtils.commonDatabaseName(
JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getDatabaseProductName));
return DatabaseDriver.fromProductName(productName);
}
catch (Exception ex) {
throw new IllegalStateException("Failed to determine DatabaseDriver", ex);
}
} This avoids adding new public API but it does leave |
If that's the only place we use this, I'd be in favor of 2. |
I'm not sure I like 1 all that much because I don't think there's an alternative return value that makes much sense. I probably favor 2, but the duplicate is a little annoying. Here's a possible third option: philwebb@1dbe863 |
Thanks, both. The 3rd option will leave us with |
@wilkinsona Is it ok to make the change here option #2 as agreed ? with a pr contribution? Is this still open? |
Thanks for the offer, @somayaj, but I don't think we've reached an agreement yet. |
+1 for option 2 then |
With a Spring Data JDBC application and this config (everything is correct except for the password):
The app fails to startup with this error message:
Caused by: java.lang.IllegalStateException: Unable to detect database type
This error makes the user think they must add
spring.datasource.type
or evenspring.datasource.driver-class-name
properties, but neither will solve it.After further investigation, the root cause in DatabaseDriver:324 is swallowed and not reported to the user. Adding a breakpoint, I can see that the root cause is in fact:
It seems like the swallow was intentional so the type can be
DatabaseDriver.UNKNOWN
. It is very misleading to the user so I would suggest that propagating the error and having startup fail because of thePSQLException
is much better than theIllegalStateException
.The text was updated successfully, but these errors were encountered: