1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -187,12 +187,14 @@ public JdbcTemplate(DataSource dataSource, boolean lazyInit) {
187
187
188
188
189
189
/**
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()
193
195
* @see java.sql.SQLWarning
194
196
* @see org.springframework.jdbc.SQLWarningException
195
- * @see #handleWarnings
197
+ * @see #handleWarnings(Statement)
196
198
*/
197
199
public void setIgnoreWarnings (boolean ignoreWarnings ) {
198
200
this .ignoreWarnings = ignoreWarnings ;
@@ -385,6 +387,9 @@ private <T> T execute(StatementCallback<T> action, boolean closeResources) throw
385
387
catch (SQLException ex ) {
386
388
// Release Connection early, to avoid potential connection pool deadlock
387
389
// in the case when the exception translator hasn't been initialized yet.
390
+ if (stmt != null ) {
391
+ handleWarnings (stmt , ex );
392
+ }
388
393
String sql = getSql (action );
389
394
JdbcUtils .closeStatement (stmt );
390
395
stmt = null ;
@@ -658,6 +663,9 @@ private <T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T>
658
663
if (psc instanceof ParameterDisposer parameterDisposer ) {
659
664
parameterDisposer .cleanupParameters ();
660
665
}
666
+ if (ps != null ) {
667
+ handleWarnings (ps , ex );
668
+ }
661
669
String sql = getSql (psc );
662
670
psc = null ;
663
671
JdbcUtils .closeStatement (ps );
@@ -1195,6 +1203,9 @@ public <T> T execute(CallableStatementCreator csc, CallableStatementCallback<T>
1195
1203
if (csc instanceof ParameterDisposer parameterDisposer ) {
1196
1204
parameterDisposer .cleanupParameters ();
1197
1205
}
1206
+ if (cs != null ) {
1207
+ handleWarnings (cs , ex );
1208
+ }
1198
1209
String sql = getSql (csc );
1199
1210
csc = null ;
1200
1211
JdbcUtils .closeStatement (cs );
@@ -1491,13 +1502,44 @@ protected PreparedStatementSetter newArgTypePreparedStatementSetter(Object[] arg
1491
1502
}
1492
1503
1493
1504
/**
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.
1496
1510
* @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)
1499
1541
*/
1500
- protected void handleWarnings (Statement stmt ) throws SQLException {
1542
+ protected void handleWarnings (Statement stmt ) throws SQLException , SQLWarningException {
1501
1543
if (isIgnoreWarnings ()) {
1502
1544
if (logger .isDebugEnabled ()) {
1503
1545
SQLWarning warningToLog = stmt .getWarnings ();
@@ -1514,7 +1556,7 @@ protected void handleWarnings(Statement stmt) throws SQLException {
1514
1556
}
1515
1557
1516
1558
/**
1517
- * Throw an SQLWarningException if encountering an actual warning.
1559
+ * Throw a {@link SQLWarningException} if encountering an actual warning.
1518
1560
* @param warning the warnings object from the current statement.
1519
1561
* May be {@code null}, in which case this method does nothing.
1520
1562
* @throws SQLWarningException in case of an actual warning to be raised
0 commit comments