Avoid getParameterType use with Oracle 12c driver by default
Issue: SPR-14629 Issue: SPR-14574 Issue: SPR-14191
This commit is contained in:
@@ -79,7 +79,7 @@ public abstract class StatementCreatorUtils {
|
|||||||
public static final String IGNORE_GETPARAMETERTYPE_PROPERTY_NAME = "spring.jdbc.getParameterType.ignore";
|
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 =
|
static final Set<String> driversWithNoSupportForGetParameterType =
|
||||||
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(1));
|
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(1));
|
||||||
@@ -89,6 +89,9 @@ public abstract class StatementCreatorUtils {
|
|||||||
private static final Map<Class<?>, Integer> javaTypeToSqlTypeMap = new HashMap<Class<?>, Integer>(32);
|
private static final Map<Class<?>, Integer> javaTypeToSqlTypeMap = new HashMap<Class<?>, Integer>(32);
|
||||||
|
|
||||||
static {
|
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(Boolean.class, Types.BOOLEAN);
|
javaTypeToSqlTypeMap.put(Boolean.class, Types.BOOLEAN);
|
||||||
javaTypeToSqlTypeMap.put(byte.class, Types.TINYINT);
|
javaTypeToSqlTypeMap.put(byte.class, Types.TINYINT);
|
||||||
@@ -246,18 +249,29 @@ public abstract class StatementCreatorUtils {
|
|||||||
Integer sqlTypeToUse = null;
|
Integer sqlTypeToUse = null;
|
||||||
DatabaseMetaData dbmd = null;
|
DatabaseMetaData dbmd = null;
|
||||||
String jdbcDriverName = null;
|
String jdbcDriverName = null;
|
||||||
boolean checkGetParameterType = !shouldIgnoreGetParameterType;
|
boolean tryGetParameterType = true;
|
||||||
if (checkGetParameterType && !driversWithNoSupportForGetParameterType.isEmpty()) {
|
|
||||||
|
if (shouldIgnoreGetParameterType == null) {
|
||||||
try {
|
try {
|
||||||
dbmd = ps.getConnection().getMetaData();
|
dbmd = ps.getConnection().getMetaData();
|
||||||
jdbcDriverName = dbmd.getDriverName();
|
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) {
|
catch (Throwable ex) {
|
||||||
logger.debug("Could not check connection metadata", ex);
|
logger.debug("Could not check connection metadata", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (checkGetParameterType) {
|
else {
|
||||||
|
tryGetParameterType = !shouldIgnoreGetParameterType;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tryGetParameterType) {
|
||||||
try {
|
try {
|
||||||
sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex);
|
sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex);
|
||||||
}
|
}
|
||||||
@@ -267,6 +281,7 @@ public abstract class StatementCreatorUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sqlTypeToUse == null) {
|
if (sqlTypeToUse == null) {
|
||||||
// JDBC driver not compliant with JDBC 3.0 -> proceed with database-specific checks
|
// JDBC driver not compliant with JDBC 3.0 -> proceed with database-specific checks
|
||||||
sqlTypeToUse = Types.NULL;
|
sqlTypeToUse = Types.NULL;
|
||||||
@@ -277,8 +292,7 @@ public abstract class StatementCreatorUtils {
|
|||||||
if (jdbcDriverName == null) {
|
if (jdbcDriverName == null) {
|
||||||
jdbcDriverName = dbmd.getDriverName();
|
jdbcDriverName = dbmd.getDriverName();
|
||||||
}
|
}
|
||||||
if (checkGetParameterType &&
|
if (shouldIgnoreGetParameterType == null) {
|
||||||
!(jdbcDriverName.startsWith("Oracle") && dbmd.getDriverMajorVersion() >= 12)) {
|
|
||||||
// Register JDBC driver with no support for getParameterType, except for the
|
// Register JDBC driver with no support for getParameterType, except for the
|
||||||
// Oracle 12c driver where getParameterType fails for specific statements only
|
// Oracle 12c driver where getParameterType fails for specific statements only
|
||||||
// (so an exception thrown above does not indicate general lack of support).
|
// (so an exception thrown above does not indicate general lack of support).
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -103,7 +103,6 @@ public class StatementCreatorUtilsTests {
|
|||||||
given(pmd.getParameterType(1)).willReturn(Types.SMALLINT);
|
given(pmd.getParameterType(1)).willReturn(Types.SMALLINT);
|
||||||
StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null);
|
StatementCreatorUtils.setParameterValue(preparedStatement, 1, SqlTypeValue.TYPE_UNKNOWN, null, null);
|
||||||
verify(pmd).getParameterType(1);
|
verify(pmd).getParameterType(1);
|
||||||
verify(preparedStatement, never()).getConnection();
|
|
||||||
verify(preparedStatement).setNull(1, Types.SMALLINT);
|
verify(preparedStatement).setNull(1, Types.SMALLINT);
|
||||||
assertTrue(StatementCreatorUtils.driversWithNoSupportForGetParameterType.isEmpty());
|
assertTrue(StatementCreatorUtils.driversWithNoSupportForGetParameterType.isEmpty());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user