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 c7b754d25ba..71a6c433fbe 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 @@ -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 driversWithNoSupportForGetParameterType = Collections.newSetFromMap(new ConcurrentHashMap(1)); @@ -89,6 +89,9 @@ public abstract class StatementCreatorUtils { private static final Map, Integer> javaTypeToSqlTypeMap = new HashMap, 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); @@ -246,18 +249,29 @@ public abstract class StatementCreatorUtils { 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); } @@ -267,6 +281,7 @@ public abstract class StatementCreatorUtils { } } } + if (sqlTypeToUse == null) { // JDBC driver not compliant with JDBC 3.0 -> proceed with database-specific checks sqlTypeToUse = Types.NULL; @@ -277,8 +292,7 @@ public abstract class StatementCreatorUtils { 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). diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/StatementCreatorUtilsTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/StatementCreatorUtilsTests.java index 1c7cb2e39e8..799d8a5def9 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/StatementCreatorUtilsTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/StatementCreatorUtilsTests.java @@ -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. @@ -103,7 +103,6 @@ public class StatementCreatorUtilsTests { 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()); }