Skip to content

Commit 036bd79

Browse files
committed
Introduced "spring.jdbc.getParameterType.ignore" property
Issue: SPR-11386 (cherry picked from commit 60c1905)
1 parent a53df0b commit 036bd79

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

spring-core/src/main/java/org/springframework/core/SpringProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @since 3.2.7
4242
* @see org.springframework.core.env.AbstractEnvironment#IGNORE_GETENV_PROPERTY_NAME
4343
* @see org.springframework.beans.CachedIntrospectionResults#IGNORE_BEANINFO_PROPERTY_NAME
44+
* @see org.springframework.jdbc.core.StatementCreatorUtils#IGNORE_GETPARAMETERTYPE_PROPERTY_NAME
4445
*/
4546
public abstract class SpringProperties {
4647

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -35,6 +35,7 @@
3535
import org.apache.commons.logging.Log;
3636
import org.apache.commons.logging.LogFactory;
3737

38+
import org.springframework.core.SpringProperties;
3839
import org.springframework.jdbc.support.SqlValue;
3940

4041
/**
@@ -59,12 +60,29 @@
5960
*/
6061
public abstract class StatementCreatorUtils {
6162

62-
private static final Log logger = LogFactory.getLog(StatementCreatorUtils.class);
63+
/**
64+
* System property that instructs Spring to ignore {@link java.sql.ParameterMetaData#getParameterType}
65+
* completely, i.e. to never even attempt to retrieve {@link PreparedStatement#getParameterMetaData()}
66+
* for {@link StatementCreatorUtils#setNull} calls.
67+
* <p>The default is "false", trying {@code getParameterType} calls first and falling back to
68+
* {@link PreparedStatement#setNull} / {@link PreparedStatement#setObject} calls based on well-known
69+
* behavior of common databases. Spring records JDBC drivers with non-working {@code getParameterType}
70+
* implementations and won't attempt to call that method for that driver again, always falling back.
71+
* <p>Consider switching this flag to "true" if you experience misbehavior at runtime, e.g. with
72+
* a connection pool setting back the {@link PreparedStatement} instance in case of an exception
73+
* thrown from {@code getParameterType} (as reported on JBoss AS 7).
74+
*/
75+
public static final String IGNORE_GETPARAMETERTYPE_PROPERTY_NAME = "spring.jdbc.getParameterType.ignore";
76+
77+
78+
static final boolean shouldIgnoreGetParameterType = SpringProperties.getFlag(IGNORE_GETPARAMETERTYPE_PROPERTY_NAME);
6379

6480
// Using a ConcurrentHashMap as a Set (for Java 5 compatibility)
6581
static final Map<String, Boolean> driversWithNoSupportForGetParameterType =
6682
new ConcurrentHashMap<String, Boolean>(1);
6783

84+
private static final Log logger = LogFactory.getLog(StatementCreatorUtils.class);
85+
6886
private static final Map<Class<?>, Integer> javaTypeToSqlTypeMap = new HashMap<Class<?>, Integer>(32);
6987

7088
static {
@@ -227,8 +245,8 @@ private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, S
227245
Integer sqlTypeToUse = null;
228246
DatabaseMetaData dbmd = null;
229247
String jdbcDriverName = null;
230-
boolean checkGetParameterType = true;
231-
if (!driversWithNoSupportForGetParameterType.isEmpty()) {
248+
boolean checkGetParameterType = !shouldIgnoreGetParameterType;
249+
if (checkGetParameterType && !driversWithNoSupportForGetParameterType.isEmpty()) {
232250
try {
233251
dbmd = ps.getConnection().getMetaData();
234252
jdbcDriverName = dbmd.getDriverName();

0 commit comments

Comments
 (0)