Skip to content

Commit f8a5aff

Browse files
committed
Merge branch '2.7.x' into 3.0.x
Closes gh-36758
2 parents 05df721 + 1e5a72f commit f8a5aff

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/DatabaseDriver.java

+2
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,9 @@ public static DatabaseDriver fromProductName(String productName) {
320320
* @param dataSource data source to inspect
321321
* @return the database driver of {@link #UNKNOWN} if not found
322322
* @since 2.6.0
323+
* @deprecated since 2.7.15 for removal in 3.3.0 with no replacement
323324
*/
325+
@Deprecated(since = "2.7.15", forRemoval = true)
324326
public static DatabaseDriver fromDataSource(DataSource dataSource) {
325327
try {
326328
String productName = JdbcUtils.commonDatabaseName(

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/init/PlatformPlaceholderDatabaseDriverResolver.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.jdbc.init;
1818

19+
import java.sql.DatabaseMetaData;
1920
import java.util.ArrayList;
2021
import java.util.Collections;
2122
import java.util.LinkedHashMap;
@@ -26,6 +27,7 @@
2627
import javax.sql.DataSource;
2728

2829
import org.springframework.boot.jdbc.DatabaseDriver;
30+
import org.springframework.jdbc.support.JdbcUtils;
2931
import org.springframework.util.Assert;
3032
import org.springframework.util.ObjectUtils;
3133
import org.springframework.util.StringUtils;
@@ -89,7 +91,6 @@ public PlatformPlaceholderDatabaseDriverResolver withDriverPlatform(DatabaseDriv
8991
* @param dataSource the DataSource from which the {@link DatabaseDriver} is derived
9092
* @param values the values in which placeholders are resolved
9193
* @return the values with their placeholders resolved
92-
* @see DatabaseDriver#fromDataSource(DataSource)
9394
*/
9495
public List<String> resolveAll(DataSource dataSource, String... values) {
9596
Assert.notNull(dataSource, "DataSource must not be null");
@@ -134,7 +135,14 @@ private String determinePlatform(DataSource dataSource) {
134135
}
135136

136137
DatabaseDriver getDatabaseDriver(DataSource dataSource) {
137-
return DatabaseDriver.fromDataSource(dataSource);
138+
try {
139+
String productName = JdbcUtils.commonDatabaseName(
140+
JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getDatabaseProductName));
141+
return DatabaseDriver.fromProductName(productName);
142+
}
143+
catch (Exception ex) {
144+
throw new IllegalStateException("Failed to determine DatabaseDriver", ex);
145+
}
138146
}
139147

140148
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/PlatformPlaceholderDatabaseDriverResolverTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static org.assertj.core.api.Assertions.assertThat;
3030
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3131
import static org.mockito.BDDMockito.given;
32+
import static org.mockito.BDDMockito.then;
3233
import static org.mockito.Mockito.mock;
3334

3435
/**
@@ -68,6 +69,24 @@ void resolveAllWithDataSourceWhenValueDoesNotContainPlaceholderShouldReturnValue
6869
.containsExactly("schema.sql");
6970
}
7071

72+
@Test
73+
void resolveAllWithDataSourceWhenValueDoesNotContainPlaceholderShouldNotInteractWithDataSource() {
74+
DataSource dataSource = mock(DataSource.class);
75+
new PlatformPlaceholderDatabaseDriverResolver().resolveAll(mock(DataSource.class), "schema.sql");
76+
then(dataSource).shouldHaveNoInteractions();
77+
}
78+
79+
@Test
80+
void resolveAllWithFailingDataSourceWhenValuesContainPlaceholdersShouldThrowNestedCause() throws SQLException {
81+
DataSource dataSource = mock(DataSource.class);
82+
given(dataSource.getConnection()).willThrow(new IllegalStateException("Test: invalid password"));
83+
assertThatIllegalStateException()
84+
.isThrownBy(() -> new PlatformPlaceholderDatabaseDriverResolver().resolveAll(dataSource, "schema.sql",
85+
"schema-@@platform@@.sql", "data-@@platform@@.sql"))
86+
.withMessage("Failed to determine DatabaseDriver")
87+
.withStackTraceContaining("Test: invalid password");
88+
}
89+
7190
@Test
7291
void resolveAllWithDataSourceWhenValuesContainPlaceholdersShouldReturnValuesWithPlaceholdersReplaced()
7392
throws SQLException {

0 commit comments

Comments
 (0)