diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java index 503a64f93e2..d3b0a98dc99 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java @@ -81,12 +81,13 @@ public abstract class StatementCreatorUtils { public static final String IGNORE_GETPARAMETERTYPE_PROPERTY_NAME = "spring.jdbc.getParameterType.ignore"; - static boolean shouldIgnoreGetParameterType = SpringProperties.getFlag(IGNORE_GETPARAMETERTYPE_PROPERTY_NAME); - private static final Log logger = LogFactory.getLog(StatementCreatorUtils.class); private static final Map, Integer> javaTypeToSqlTypeMap = new HashMap<>(64); + @Nullable + static Boolean shouldIgnoreGetParameterType; + static { javaTypeToSqlTypeMap.put(boolean.class, Types.BOOLEAN); javaTypeToSqlTypeMap.put(Boolean.class, Types.BOOLEAN); @@ -114,6 +115,11 @@ public abstract class StatementCreatorUtils { javaTypeToSqlTypeMap.put(java.sql.Timestamp.class, Types.TIMESTAMP); javaTypeToSqlTypeMap.put(Blob.class, Types.BLOB); javaTypeToSqlTypeMap.put(Clob.class, Types.CLOB); + + String flag = SpringProperties.getProperty(IGNORE_GETPARAMETERTYPE_PROPERTY_NAME); + if (flag != null) { + shouldIgnoreGetParameterType = Boolean.valueOf(flag); + } } @@ -250,9 +256,26 @@ public abstract class StatementCreatorUtils { throws SQLException { if (sqlType == SqlTypeValue.TYPE_UNKNOWN || (sqlType == Types.OTHER && typeName == null)) { + boolean callGetParameterType = false; boolean useSetObject = false; Integer sqlTypeToUse = null; - if (!shouldIgnoreGetParameterType) { + if (shouldIgnoreGetParameterType != null) { + callGetParameterType = !shouldIgnoreGetParameterType; + } + else { + String jdbcDriverName = ps.getConnection().getMetaData().getDriverName(); + if (jdbcDriverName.startsWith("PostgreSQL")) { + sqlTypeToUse = Types.NULL; + } + else if (jdbcDriverName.startsWith("Microsoft") && jdbcDriverName.contains("SQL Server")) { + sqlTypeToUse = Types.NULL; + useSetObject = true; + } + else { + callGetParameterType = true; + } + } + if (callGetParameterType) { try { sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex); }