Browse Source

Correctly handle NVARCHAR, LONGNVARCHAR and NCLOBs

Issue: SPR-16154
pull/1610/head
Juergen Hoeller 8 years ago
parent
commit
d5f34ed641
  1. 20
      spring-jdbc/src/main/java/org/springframework/jdbc/core/StatementCreatorUtils.java

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

@ -68,13 +68,16 @@ public abstract class StatementCreatorUtils {
* System property that instructs Spring to ignore {@link java.sql.ParameterMetaData#getParameterType} * System property that instructs Spring to ignore {@link java.sql.ParameterMetaData#getParameterType}
* completely, i.e. to never even attempt to retrieve {@link PreparedStatement#getParameterMetaData()} * completely, i.e. to never even attempt to retrieve {@link PreparedStatement#getParameterMetaData()}
* for {@link StatementCreatorUtils#setNull} calls. * for {@link StatementCreatorUtils#setNull} calls.
* <p>The default is "false", trying {@code getParameterType} calls first and falling back to * <p>The effective default is "false", trying {@code getParameterType} calls first and falling back to
* {@link PreparedStatement#setNull} / {@link PreparedStatement#setObject} calls based on well-known * {@link PreparedStatement#setNull} / {@link PreparedStatement#setObject} calls based on well-known
* behavior of common databases. Spring records JDBC drivers with non-working {@code getParameterType} * behavior of common databases. Spring records JDBC drivers with non-working {@code getParameterType}
* implementations and won't attempt to call that method for that driver again, always falling back. * implementations and won't attempt to call that method for that driver again, always falling back.
* <p>Consider switching this flag to "true" if you experience misbehavior at runtime, e.g. with * <p>Consider switching this flag to "true" if you experience misbehavior at runtime, e.g. with
* a connection pool setting back the {@link PreparedStatement} instance in case of an exception * a connection pool setting back the {@link PreparedStatement} instance in case of an exception
* thrown from {@code getParameterType} (as reported on JBoss AS 7). * thrown from {@code getParameterType} (as reported on JBoss AS 7).
* <p>Note that this flag is "true" by default on Oracle 12c since there can be leaks created by
* {@code getParameterType} calls in such a scenario. You need to explicitly set the flag to
* "false" in order to enforce the use of {@code getParameterType} against Oracle drivers.
*/ */
public static final String IGNORE_GETPARAMETERTYPE_PROPERTY_NAME = "spring.jdbc.getParameterType.ignore"; public static final String IGNORE_GETPARAMETERTYPE_PROPERTY_NAME = "spring.jdbc.getParameterType.ignore";
@ -339,10 +342,12 @@ public abstract class StatementCreatorUtils {
else if (inValue instanceof SqlValue) { else if (inValue instanceof SqlValue) {
((SqlValue) inValue).setValue(ps, paramIndex); ((SqlValue) inValue).setValue(ps, paramIndex);
} }
else if (sqlType == Types.VARCHAR || sqlType == Types.NVARCHAR || else if (sqlType == Types.VARCHAR || sqlType == Types.LONGVARCHAR ) {
sqlType == Types.LONGVARCHAR || sqlType == Types.LONGNVARCHAR) {
ps.setString(paramIndex, inValue.toString()); ps.setString(paramIndex, inValue.toString());
} }
else if (sqlType == Types.NVARCHAR || sqlType == Types.LONGNVARCHAR) {
ps.setNString(paramIndex, inValue.toString());
}
else if ((sqlType == Types.CLOB || sqlType == Types.NCLOB) && isStringValue(inValue.getClass())) { else if ((sqlType == Types.CLOB || sqlType == Types.NCLOB) && isStringValue(inValue.getClass())) {
String strVal = inValue.toString(); String strVal = inValue.toString();
if (strVal.length() > 4000) { if (strVal.length() > 4000) {
@ -364,8 +369,13 @@ public abstract class StatementCreatorUtils {
logger.debug("JDBC driver does not support JDBC 4.0 'setClob(int, Reader, long)' method", ex); logger.debug("JDBC driver does not support JDBC 4.0 'setClob(int, Reader, long)' method", ex);
} }
} }
// Fallback: regular setString binding // Fallback: setString or setNString binding
ps.setString(paramIndex, strVal); if (sqlType == Types.NCLOB) {
ps.setNString(paramIndex, strVal);
}
else {
ps.setString(paramIndex, strVal);
}
} }
else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) { else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) {
if (inValue instanceof BigDecimal) { if (inValue instanceof BigDecimal) {

Loading…
Cancel
Save