Skip to content

Commit 3b899fe

Browse files
committed
Handle JDBC warnings in case of a statement exception as well
Closes gh-23106
1 parent c91041b commit 3b899fe

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-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.
@@ -44,11 +44,22 @@ public SQLWarningException(String msg, SQLWarning ex) {
4444
super(msg, ex);
4545
}
4646

47+
4748
/**
48-
* Return the underlying SQLWarning.
49+
* Return the underlying {@link SQLWarning}.
50+
* @since 5.3.29
4951
*/
50-
public SQLWarning SQLWarning() {
52+
public SQLWarning getSQLWarning() {
5153
return (SQLWarning) getCause();
5254
}
5355

56+
/**
57+
* Return the underlying {@link SQLWarning}.
58+
* @deprecated as of 5.3.29, in favor of {@link #getSQLWarning()}
59+
*/
60+
@Deprecated(since = "5.3.29")
61+
public SQLWarning SQLWarning() {
62+
return getSQLWarning();
63+
}
64+
5465
}

spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-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.
@@ -187,12 +187,14 @@ public JdbcTemplate(DataSource dataSource, boolean lazyInit) {
187187

188188

189189
/**
190-
* Set whether we want to ignore SQLWarnings.
191-
* <p>Default is "true", swallowing and logging all warnings. Switch this flag
192-
* to "false" to make the JdbcTemplate throw an SQLWarningException instead.
190+
* Set whether we want to ignore JDBC statement warnings ({@link SQLWarning}).
191+
* <p>Default is {@code true}, swallowing and logging all warnings. Switch this flag to
192+
* {@code false} to make this JdbcTemplate throw a {@link SQLWarningException} instead
193+
* (or chain the {@link SQLWarning} into the primary {@link SQLException}, if any).
194+
* @see Statement#getWarnings()
193195
* @see java.sql.SQLWarning
194196
* @see org.springframework.jdbc.SQLWarningException
195-
* @see #handleWarnings
197+
* @see #handleWarnings(Statement)
196198
*/
197199
public void setIgnoreWarnings(boolean ignoreWarnings) {
198200
this.ignoreWarnings = ignoreWarnings;
@@ -385,6 +387,9 @@ private <T> T execute(StatementCallback<T> action, boolean closeResources) throw
385387
catch (SQLException ex) {
386388
// Release Connection early, to avoid potential connection pool deadlock
387389
// in the case when the exception translator hasn't been initialized yet.
390+
if (stmt != null) {
391+
handleWarnings(stmt, ex);
392+
}
388393
String sql = getSql(action);
389394
JdbcUtils.closeStatement(stmt);
390395
stmt = null;
@@ -658,6 +663,9 @@ private <T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T>
658663
if (psc instanceof ParameterDisposer parameterDisposer) {
659664
parameterDisposer.cleanupParameters();
660665
}
666+
if (ps != null) {
667+
handleWarnings(ps, ex);
668+
}
661669
String sql = getSql(psc);
662670
psc = null;
663671
JdbcUtils.closeStatement(ps);
@@ -1195,6 +1203,9 @@ public <T> T execute(CallableStatementCreator csc, CallableStatementCallback<T>
11951203
if (csc instanceof ParameterDisposer parameterDisposer) {
11961204
parameterDisposer.cleanupParameters();
11971205
}
1206+
if (cs != null) {
1207+
handleWarnings(cs, ex);
1208+
}
11981209
String sql = getSql(csc);
11991210
csc = null;
12001211
JdbcUtils.closeStatement(cs);
@@ -1491,13 +1502,44 @@ protected PreparedStatementSetter newArgTypePreparedStatementSetter(Object[] arg
14911502
}
14921503

14931504
/**
1494-
* Throw an SQLWarningException if we're not ignoring warnings,
1495-
* otherwise log the warnings at debug level.
1505+
* Handle warnings before propagating a primary {@code SQLException}
1506+
* from executing the given statement.
1507+
* <p>Calls regular {@link #handleWarnings(Statement)} but catches
1508+
* {@link SQLWarningException} in order to chain the {@link SQLWarning}
1509+
* into the primary exception instead.
14961510
* @param stmt the current JDBC statement
1497-
* @throws SQLWarningException if not ignoring warnings
1498-
* @see org.springframework.jdbc.SQLWarningException
1511+
* @param ex the primary exception after failed statement execution
1512+
* @since 5.3.29
1513+
* @see #handleWarnings(Statement)
1514+
* @see SQLException#setNextException
1515+
*/
1516+
protected void handleWarnings(Statement stmt, SQLException ex) {
1517+
try {
1518+
handleWarnings(stmt);
1519+
}
1520+
catch (SQLWarningException nonIgnoredWarning) {
1521+
ex.setNextException(nonIgnoredWarning.getSQLWarning());
1522+
}
1523+
catch (SQLException warningsEx) {
1524+
logger.debug("Failed to retrieve warnings", warningsEx);
1525+
}
1526+
catch (Throwable warningsEx) {
1527+
logger.debug("Failed to process warnings", warningsEx);
1528+
}
1529+
}
1530+
1531+
/**
1532+
* Handle the warnings for the given JDBC statement, if any.
1533+
* <p>Throws a {@link SQLWarningException} if we're not ignoring warnings,
1534+
* otherwise logs the warnings at debug level.
1535+
* @param stmt the current JDBC statement
1536+
* @throws SQLException in case of warnings retrieval failure
1537+
* @throws SQLWarningException for a concrete warning to raise
1538+
* (when not ignoring warnings)
1539+
* @see #setIgnoreWarnings
1540+
* @see #handleWarnings(SQLWarning)
14991541
*/
1500-
protected void handleWarnings(Statement stmt) throws SQLException {
1542+
protected void handleWarnings(Statement stmt) throws SQLException, SQLWarningException {
15011543
if (isIgnoreWarnings()) {
15021544
if (logger.isDebugEnabled()) {
15031545
SQLWarning warningToLog = stmt.getWarnings();
@@ -1514,7 +1556,7 @@ protected void handleWarnings(Statement stmt) throws SQLException {
15141556
}
15151557

15161558
/**
1517-
* Throw an SQLWarningException if encountering an actual warning.
1559+
* Throw a {@link SQLWarningException} if encountering an actual warning.
15181560
* @param warning the warnings object from the current statement.
15191561
* May be {@code null}, in which case this method does nothing.
15201562
* @throws SQLWarningException in case of an actual warning to be raised

0 commit comments

Comments
 (0)