Do not retain partial column metadata in SimpleJdbcInsert
Prior to this commit, if an SQLException was thrown while retrieving column metadata from the database, SimpleJdbcInsert would generate an INSERT statement that was syntactically valid but missing columns, which could lead to data silently missing in the database (for nullable columns). This commit fixes this by clearing all collected column metadata if an SQLException is thrown while processing the metadata. The result is that an InvalidDataAccessApiUsageException will be thrown later while generating the INSERT statement. The exception message now contains an additional hint to make use of SimpleJdbcInsert#usingColumns() in order to ensure that all required columns are included in the generated INSERT statement. SimpleJdbcCall can also encounter an SQLException while retrieving column metadata for a stored procedure/function, but an exception is not thrown since a later invocation of the stored procedure/function will likely fail anyway due to missing arguments. Consequently, this commit only improves the warning level log message by including a hint to make use of SimpleJdbcCall#addDeclaredParameter(). Closes gh-26486
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,6 +39,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class GenericCallMetaDataProvider implements CallMetaDataProvider {
|
||||
@@ -414,8 +415,15 @@ public class GenericCallMetaDataProvider implements CallMetaDataProvider {
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Error while retrieving meta-data for procedure columns: " + ex);
|
||||
logger.warn("Error while retrieving meta-data for procedure columns. " +
|
||||
"Consider declaring explicit parameters -- for example, via SimpleJdbcCall#addDeclaredParameter().",
|
||||
ex);
|
||||
}
|
||||
// Although we could invoke `this.callParameterMetaData.clear()` so that
|
||||
// we don't retain a partial list of column names (like we do in
|
||||
// GenericTableMetaDataProvider.processTableColumns(...)), we choose
|
||||
// not to do that here, since invocation of the stored procedure will
|
||||
// likely fail anyway with an incorrect argument list.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,6 +39,7 @@ import org.springframework.lang.Nullable;
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class GenericTableMetaDataProvider implements TableMetaDataProvider {
|
||||
@@ -422,8 +423,12 @@ public class GenericTableMetaDataProvider implements TableMetaDataProvider {
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Error while retrieving meta-data for table columns: " + ex.getMessage());
|
||||
logger.warn("Error while retrieving meta-data for table columns. " +
|
||||
"Consider specifying explicit column names -- for example, via SimpleJdbcInsert#usingColumns().",
|
||||
ex);
|
||||
}
|
||||
// Clear the metadata so that we don't retain a partial list of column names
|
||||
this.tableParameterMetaData.clear();
|
||||
}
|
||||
finally {
|
||||
JdbcUtils.closeResultSet(tableColumns);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -43,6 +43,7 @@ import org.springframework.util.CollectionUtils;
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
*/
|
||||
public class TableMetaDataContext {
|
||||
@@ -302,8 +303,12 @@ public class TableMetaDataContext {
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new InvalidDataAccessApiUsageException("Unable to locate columns for table '" +
|
||||
getTableName() + "' so an insert statement can't be generated");
|
||||
String message = "Unable to locate columns for table '" + getTableName()
|
||||
+ "' so an insert statement can't be generated.";
|
||||
if (isAccessTableColumnMetaData()) {
|
||||
message += " Consider specifying explicit column names -- for example, via SimpleJdbcInsert#usingColumns().";
|
||||
}
|
||||
throw new InvalidDataAccessApiUsageException(message);
|
||||
}
|
||||
}
|
||||
String params = String.join(", ", Collections.nCopies(columnCount, "?"));
|
||||
|
||||
Reference in New Issue
Block a user