Optimized use of JDBC 3.0 ParameterMetaData.getParameterType, caching information about drivers which do not support that feature
Issue: SPR-11100
This commit is contained in:
@@ -28,8 +28,11 @@ import java.sql.Types;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -60,7 +63,10 @@ public abstract class StatementCreatorUtils {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(StatementCreatorUtils.class);
|
||||
|
||||
private static Map<Class<?>, Integer> javaTypeToSqlTypeMap = new HashMap<Class<?>, Integer>(32);
|
||||
static final Set<String> driversWithNoSupportForGetParameterType =
|
||||
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(1));
|
||||
|
||||
private static final Map<Class<?>, Integer> javaTypeToSqlTypeMap = new HashMap<Class<?>, Integer>(32);
|
||||
|
||||
static {
|
||||
/* JDBC 3.0 only - not compatible with e.g. MySQL at present
|
||||
@@ -219,19 +225,44 @@ public abstract class StatementCreatorUtils {
|
||||
private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, String typeName) throws SQLException {
|
||||
if (sqlType == SqlTypeValue.TYPE_UNKNOWN) {
|
||||
boolean useSetObject = false;
|
||||
sqlType = Types.NULL;
|
||||
try {
|
||||
sqlType = ps.getParameterMetaData().getParameterType(paramIndex);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("JDBC 3.0 getParameterType call not supported - using fallback method instead: " + ex);
|
||||
}
|
||||
// JDBC driver not compliant with JDBC 3.0 -> proceed with database-specific checks
|
||||
Integer sqlTypeToUse = null;
|
||||
DatabaseMetaData dbmd = null;
|
||||
String jdbcDriverName = null;
|
||||
boolean checkGetParameterType = true;
|
||||
if (!driversWithNoSupportForGetParameterType.isEmpty()) {
|
||||
try {
|
||||
DatabaseMetaData dbmd = ps.getConnection().getMetaData();
|
||||
dbmd = ps.getConnection().getMetaData();
|
||||
jdbcDriverName = dbmd.getDriverName();
|
||||
checkGetParameterType = !driversWithNoSupportForGetParameterType.contains(jdbcDriverName);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
logger.debug("Could not check connection metadata", ex);
|
||||
}
|
||||
}
|
||||
if (checkGetParameterType) {
|
||||
try {
|
||||
sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex);
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("JDBC 3.0 getParameterType call not supported - using fallback method instead: " + ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sqlTypeToUse == null) {
|
||||
// JDBC driver not compliant with JDBC 3.0 -> proceed with database-specific checks
|
||||
sqlTypeToUse = Types.NULL;
|
||||
try {
|
||||
if (dbmd == null) {
|
||||
dbmd = ps.getConnection().getMetaData();
|
||||
}
|
||||
if (jdbcDriverName == null) {
|
||||
jdbcDriverName = dbmd.getDriverName();
|
||||
}
|
||||
if (checkGetParameterType) {
|
||||
driversWithNoSupportForGetParameterType.add(jdbcDriverName);
|
||||
}
|
||||
String databaseProductName = dbmd.getDatabaseProductName();
|
||||
String jdbcDriverName = dbmd.getDriverName();
|
||||
if (databaseProductName.startsWith("Informix") ||
|
||||
jdbcDriverName.startsWith("Microsoft SQL Server")) {
|
||||
useSetObject = true;
|
||||
@@ -240,18 +271,18 @@ public abstract class StatementCreatorUtils {
|
||||
jdbcDriverName.startsWith("jConnect") ||
|
||||
jdbcDriverName.startsWith("SQLServer")||
|
||||
jdbcDriverName.startsWith("Apache Derby")) {
|
||||
sqlType = Types.VARCHAR;
|
||||
sqlTypeToUse = Types.VARCHAR;
|
||||
}
|
||||
}
|
||||
catch (Throwable ex2) {
|
||||
logger.debug("Could not check database or driver name", ex2);
|
||||
catch (Throwable ex) {
|
||||
logger.debug("Could not check connection metadata", ex);
|
||||
}
|
||||
}
|
||||
if (useSetObject) {
|
||||
ps.setObject(paramIndex, null);
|
||||
}
|
||||
else {
|
||||
ps.setNull(paramIndex, sqlType);
|
||||
ps.setNull(paramIndex, sqlTypeToUse);
|
||||
}
|
||||
}
|
||||
else if (typeName != null) {
|
||||
|
||||
Reference in New Issue
Block a user