Skip to content

Commit

Permalink
Avoid getParameterType use with Oracle 12c driver by default
Browse files Browse the repository at this point in the history
Issue: SPR-14629
Issue: SPR-14574
Issue: SPR-14191
(cherry picked from commit 52447ef)
  • Loading branch information
jhoeller committed Aug 26, 2016
1 parent d03afeb commit 2adbfb6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Expand Up @@ -79,7 +79,7 @@ public abstract class StatementCreatorUtils {
public static final String IGNORE_GETPARAMETERTYPE_PROPERTY_NAME = "spring.jdbc.getParameterType.ignore";


static final boolean shouldIgnoreGetParameterType = SpringProperties.getFlag(IGNORE_GETPARAMETERTYPE_PROPERTY_NAME);
static final Boolean shouldIgnoreGetParameterType;

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

static {
String propVal = SpringProperties.getProperty(IGNORE_GETPARAMETERTYPE_PROPERTY_NAME);
shouldIgnoreGetParameterType = (propVal != null ? Boolean.valueOf(propVal) : null);

javaTypeToSqlTypeMap.put(boolean.class, Types.BOOLEAN);
javaTypeToSqlTypeMap.put(Boolean.class, Types.BOOLEAN);
javaTypeToSqlTypeMap.put(byte.class, Types.TINYINT);
Expand Down Expand Up @@ -246,18 +249,29 @@ private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, S
Integer sqlTypeToUse = null;
DatabaseMetaData dbmd = null;
String jdbcDriverName = null;
boolean checkGetParameterType = !shouldIgnoreGetParameterType;
if (checkGetParameterType && !driversWithNoSupportForGetParameterType.isEmpty()) {
boolean tryGetParameterType = true;

if (shouldIgnoreGetParameterType == null) {
try {
dbmd = ps.getConnection().getMetaData();
jdbcDriverName = dbmd.getDriverName();
checkGetParameterType = !driversWithNoSupportForGetParameterType.contains(jdbcDriverName);
tryGetParameterType = !driversWithNoSupportForGetParameterType.contains(jdbcDriverName);
if (tryGetParameterType && jdbcDriverName.startsWith("Oracle")) {
// Avoid getParameterType use with Oracle 12c driver by default:
// needs to be explicitly activated through spring.jdbc.getParameterType.ignore=false
tryGetParameterType = false;
driversWithNoSupportForGetParameterType.add(jdbcDriverName);
}
}
catch (Throwable ex) {
logger.debug("Could not check connection metadata", ex);
}
}
if (checkGetParameterType) {
else {
tryGetParameterType = !shouldIgnoreGetParameterType;
}

if (tryGetParameterType) {
try {
sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex);
}
Expand All @@ -267,6 +281,7 @@ private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, S
}
}
}

if (sqlTypeToUse == null) {
// JDBC driver not compliant with JDBC 3.0 -> proceed with database-specific checks
sqlTypeToUse = Types.NULL;
Expand All @@ -277,8 +292,7 @@ private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, S
if (jdbcDriverName == null) {
jdbcDriverName = dbmd.getDriverName();
}
if (checkGetParameterType &&
!(jdbcDriverName.startsWith("Oracle") && dbmd.getDriverMajorVersion() >= 12)) {
if (shouldIgnoreGetParameterType == null) {
// Register JDBC driver with no support for getParameterType, except for the
// Oracle 12c driver where getParameterType fails for specific statements only
// (so an exception thrown above does not indicate general lack of support).
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
Expand Down Expand Up @@ -103,7 +103,6 @@ public void testSetParameterValueWithNullAndGetParameterTypeWorking() throws SQL
given(pmd.getParameterType(1)).willReturn(Types.SMALLINT);
StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null);
verify(pmd).getParameterType(1);
verify(preparedStatement, never()).getConnection();
verify(preparedStatement).setNull(1, Types.SMALLINT);
assertTrue(StatementCreatorUtils.driversWithNoSupportForGetParameterType.isEmpty());
}
Expand Down

0 comments on commit 2adbfb6

Please sign in to comment.