Skip to content

Commit a4ccec1

Browse files
committed
Merge branch '5.3.x'
2 parents 0783f07 + 5639ff0 commit a4ccec1

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/CannotGetJdbcConnectionException.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -48,4 +48,12 @@ public CannotGetJdbcConnectionException(String msg, @Nullable SQLException ex) {
4848
super(msg, ex);
4949
}
5050

51+
/**
52+
* Constructor for CannotGetJdbcConnectionException.
53+
* @param msg the detail message
54+
* @param ex the root cause IllegalStateException
55+
*/
56+
public CannotGetJdbcConnectionException(String msg, IllegalStateException ex) {
57+
super(msg, ex);
58+
}
5159
}

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static Connection getConnection(DataSource dataSource) throws CannotGetJd
8383
throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex);
8484
}
8585
catch (IllegalStateException ex) {
86-
throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection: " + ex.getMessage());
86+
throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex);
8787
}
8888
}
8989

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.jdbc.datasource;
18+
19+
import java.sql.SQLException;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.jdbc.CannotGetJdbcConnectionException;
26+
27+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
28+
import static org.mockito.BDDMockito.when;
29+
import static org.mockito.Mockito.mock;
30+
31+
/**
32+
* Tests for {@link DataSourceUtils}.
33+
*
34+
* @author Kevin Schoenfeld
35+
* @author Stephane Nicoll
36+
*/
37+
class DataSourceUtilsTests {
38+
39+
@Test
40+
void testConnectionNotAcquiredExceptionIsPropagated() throws SQLException {
41+
DataSource dataSource = mock(DataSource.class);
42+
when(dataSource.getConnection()).thenReturn(null);
43+
assertThatThrownBy(() -> DataSourceUtils.getConnection(dataSource))
44+
.isInstanceOf(CannotGetJdbcConnectionException.class)
45+
.hasMessageStartingWith("Failed to obtain JDBC Connection")
46+
.hasCauseInstanceOf(IllegalStateException.class);
47+
}
48+
49+
@Test
50+
void testConnectionSQLExceptionIsPropagated() throws SQLException {
51+
DataSource dataSource = mock(DataSource.class);
52+
when(dataSource.getConnection()).thenThrow(new SQLException("my dummy exception"));
53+
assertThatThrownBy(() -> DataSourceUtils.getConnection(dataSource))
54+
.isInstanceOf(CannotGetJdbcConnectionException.class)
55+
.hasMessageStartingWith("Failed to obtain JDBC Connection")
56+
.cause().isInstanceOf(SQLException.class)
57+
.hasMessage("my dummy exception");
58+
}
59+
60+
}
61+

0 commit comments

Comments
 (0)