Skip to content

Commit 2adbfb6

Browse files
committed
Avoid getParameterType use with Oracle 12c driver by default
Issue: SPR-14629 Issue: SPR-14574 Issue: SPR-14191 (cherry picked from commit 52447ef)
1 parent d03afeb commit 2adbfb6

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public abstract class StatementCreatorUtils {
7979
public static final String IGNORE_GETPARAMETERTYPE_PROPERTY_NAME = "spring.jdbc.getParameterType.ignore";
8080

8181

82-
static final boolean shouldIgnoreGetParameterType = SpringProperties.getFlag(IGNORE_GETPARAMETERTYPE_PROPERTY_NAME);
82+
static final Boolean shouldIgnoreGetParameterType;
8383

8484
static final Set<String> driversWithNoSupportForGetParameterType =
8585
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(1));
@@ -89,6 +89,9 @@ public abstract class StatementCreatorUtils {
8989
private static final Map<Class<?>, Integer> javaTypeToSqlTypeMap = new HashMap<Class<?>, Integer>(32);
9090

9191
static {
92+
String propVal = SpringProperties.getProperty(IGNORE_GETPARAMETERTYPE_PROPERTY_NAME);
93+
shouldIgnoreGetParameterType = (propVal != null ? Boolean.valueOf(propVal) : null);
94+
9295
javaTypeToSqlTypeMap.put(boolean.class, Types.BOOLEAN);
9396
javaTypeToSqlTypeMap.put(Boolean.class, Types.BOOLEAN);
9497
javaTypeToSqlTypeMap.put(byte.class, Types.TINYINT);
@@ -246,18 +249,29 @@ private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, S
246249
Integer sqlTypeToUse = null;
247250
DatabaseMetaData dbmd = null;
248251
String jdbcDriverName = null;
249-
boolean checkGetParameterType = !shouldIgnoreGetParameterType;
250-
if (checkGetParameterType && !driversWithNoSupportForGetParameterType.isEmpty()) {
252+
boolean tryGetParameterType = true;
253+
254+
if (shouldIgnoreGetParameterType == null) {
251255
try {
252256
dbmd = ps.getConnection().getMetaData();
253257
jdbcDriverName = dbmd.getDriverName();
254-
checkGetParameterType = !driversWithNoSupportForGetParameterType.contains(jdbcDriverName);
258+
tryGetParameterType = !driversWithNoSupportForGetParameterType.contains(jdbcDriverName);
259+
if (tryGetParameterType && jdbcDriverName.startsWith("Oracle")) {
260+
// Avoid getParameterType use with Oracle 12c driver by default:
261+
// needs to be explicitly activated through spring.jdbc.getParameterType.ignore=false
262+
tryGetParameterType = false;
263+
driversWithNoSupportForGetParameterType.add(jdbcDriverName);
264+
}
255265
}
256266
catch (Throwable ex) {
257267
logger.debug("Could not check connection metadata", ex);
258268
}
259269
}
260-
if (checkGetParameterType) {
270+
else {
271+
tryGetParameterType = !shouldIgnoreGetParameterType;
272+
}
273+
274+
if (tryGetParameterType) {
261275
try {
262276
sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex);
263277
}
@@ -267,6 +281,7 @@ private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, S
267281
}
268282
}
269283
}
284+
270285
if (sqlTypeToUse == null) {
271286
// JDBC driver not compliant with JDBC 3.0 -> proceed with database-specific checks
272287
sqlTypeToUse = Types.NULL;
@@ -277,8 +292,7 @@ private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, S
277292
if (jdbcDriverName == null) {
278293
jdbcDriverName = dbmd.getDriverName();
279294
}
280-
if (checkGetParameterType &&
281-
!(jdbcDriverName.startsWith("Oracle") && dbmd.getDriverMajorVersion() >= 12)) {
295+
if (shouldIgnoreGetParameterType == null) {
282296
// Register JDBC driver with no support for getParameterType, except for the
283297
// Oracle 12c driver where getParameterType fails for specific statements only
284298
// (so an exception thrown above does not indicate general lack of support).

spring-jdbc/src/test/java/org/springframework/jdbc/core/StatementCreatorUtilsTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -103,7 +103,6 @@ public void testSetParameterValueWithNullAndGetParameterTypeWorking() throws SQL
103103
given(pmd.getParameterType(1)).willReturn(Types.SMALLINT);
104104
StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null);
105105
verify(pmd).getParameterType(1);
106-
verify(preparedStatement, never()).getConnection();
107106
verify(preparedStatement).setNull(1, Types.SMALLINT);
108107
assertTrue(StatementCreatorUtils.driversWithNoSupportForGetParameterType.isEmpty());
109108
}

0 commit comments

Comments
 (0)