Skip to content

Commit 77b0382

Browse files
committed
Bypass getParameterType by default for PostgreSQL and SQL Server drivers
Closes gh-25679
1 parent a7f9da1 commit 77b0382

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ public abstract class StatementCreatorUtils {
8181
public static final String IGNORE_GETPARAMETERTYPE_PROPERTY_NAME = "spring.jdbc.getParameterType.ignore";
8282

8383

84-
static boolean shouldIgnoreGetParameterType = SpringProperties.getFlag(IGNORE_GETPARAMETERTYPE_PROPERTY_NAME);
85-
8684
private static final Log logger = LogFactory.getLog(StatementCreatorUtils.class);
8785

8886
private static final Map<Class<?>, Integer> javaTypeToSqlTypeMap = new HashMap<>(64);
8987

88+
@Nullable
89+
static Boolean shouldIgnoreGetParameterType;
90+
9091
static {
9192
javaTypeToSqlTypeMap.put(boolean.class, Types.BOOLEAN);
9293
javaTypeToSqlTypeMap.put(Boolean.class, Types.BOOLEAN);
@@ -114,6 +115,11 @@ public abstract class StatementCreatorUtils {
114115
javaTypeToSqlTypeMap.put(java.sql.Timestamp.class, Types.TIMESTAMP);
115116
javaTypeToSqlTypeMap.put(Blob.class, Types.BLOB);
116117
javaTypeToSqlTypeMap.put(Clob.class, Types.CLOB);
118+
119+
String flag = SpringProperties.getProperty(IGNORE_GETPARAMETERTYPE_PROPERTY_NAME);
120+
if (flag != null) {
121+
shouldIgnoreGetParameterType = Boolean.valueOf(flag);
122+
}
117123
}
118124

119125

@@ -250,9 +256,26 @@ private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, @
250256
throws SQLException {
251257

252258
if (sqlType == SqlTypeValue.TYPE_UNKNOWN || (sqlType == Types.OTHER && typeName == null)) {
259+
boolean callGetParameterType = false;
253260
boolean useSetObject = false;
254261
Integer sqlTypeToUse = null;
255-
if (!shouldIgnoreGetParameterType) {
262+
if (shouldIgnoreGetParameterType != null) {
263+
callGetParameterType = !shouldIgnoreGetParameterType;
264+
}
265+
else {
266+
String jdbcDriverName = ps.getConnection().getMetaData().getDriverName();
267+
if (jdbcDriverName.startsWith("PostgreSQL")) {
268+
sqlTypeToUse = Types.NULL;
269+
}
270+
else if (jdbcDriverName.startsWith("Microsoft") && jdbcDriverName.contains("SQL Server")) {
271+
sqlTypeToUse = Types.NULL;
272+
useSetObject = true;
273+
}
274+
else {
275+
callGetParameterType = true;
276+
}
277+
}
278+
if (callGetParameterType) {
256279
try {
257280
sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex);
258281
}

0 commit comments

Comments
 (0)