Search in sources :

Example 11 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-framework by spring-projects.

the class ChainedPersistenceExceptionTranslatorTests method exceptionTranslationWithTranslation.

@Test
public void exceptionTranslationWithTranslation() {
    MapPersistenceExceptionTranslator mpet1 = new MapPersistenceExceptionTranslator();
    RuntimeException in1 = new RuntimeException("in");
    InvalidDataAccessApiUsageException out1 = new InvalidDataAccessApiUsageException("out");
    InvalidDataAccessApiUsageException out2 = new InvalidDataAccessApiUsageException("out");
    mpet1.addTranslation(in1, out1);
    ChainedPersistenceExceptionTranslator chainedPet1 = new ChainedPersistenceExceptionTranslator();
    assertSame("Should not translate yet", in1, DataAccessUtils.translateIfNecessary(in1, chainedPet1));
    chainedPet1.addDelegate(mpet1);
    assertSame("Should now translate", out1, DataAccessUtils.translateIfNecessary(in1, chainedPet1));
    // Now add a new translator and verify it wins
    MapPersistenceExceptionTranslator mpet2 = new MapPersistenceExceptionTranslator();
    mpet2.addTranslation(in1, out2);
    chainedPet1.addDelegate(mpet2);
    assertSame("Should still translate the same due to ordering", out1, DataAccessUtils.translateIfNecessary(in1, chainedPet1));
    ChainedPersistenceExceptionTranslator chainedPet2 = new ChainedPersistenceExceptionTranslator();
    chainedPet2.addDelegate(mpet2);
    chainedPet2.addDelegate(mpet1);
    assertSame("Should translate differently due to ordering", out2, DataAccessUtils.translateIfNecessary(in1, chainedPet2));
    RuntimeException in2 = new RuntimeException("in2");
    OptimisticLockingFailureException out3 = new OptimisticLockingFailureException("out2");
    assertNull(chainedPet2.translateExceptionIfPossible(in2));
    MapPersistenceExceptionTranslator mpet3 = new MapPersistenceExceptionTranslator();
    mpet3.addTranslation(in2, out3);
    chainedPet2.addDelegate(mpet3);
    assertSame(out3, chainedPet2.translateExceptionIfPossible(in2));
}
Also used : OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) MapPersistenceExceptionTranslator(org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) Test(org.junit.Test)

Example 12 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-framework by spring-projects.

the class GenericCallMetaDataProvider method processProcedureColumns.

/**
	 * Process the procedure column metadata
	 */
private void processProcedureColumns(DatabaseMetaData databaseMetaData, String catalogName, String schemaName, String procedureName) {
    String metaDataCatalogName = metaDataCatalogNameToUse(catalogName);
    String metaDataSchemaName = metaDataSchemaNameToUse(schemaName);
    String metaDataProcedureName = procedureNameToUse(procedureName);
    if (logger.isDebugEnabled()) {
        logger.debug("Retrieving metadata for " + metaDataCatalogName + '/' + metaDataSchemaName + '/' + metaDataProcedureName);
    }
    ResultSet procs = null;
    try {
        procs = databaseMetaData.getProcedures(metaDataCatalogName, metaDataSchemaName, metaDataProcedureName);
        List<String> found = new ArrayList<>();
        while (procs.next()) {
            found.add(procs.getString("PROCEDURE_CAT") + '.' + procs.getString("PROCEDURE_SCHEM") + '.' + procs.getString("PROCEDURE_NAME"));
        }
        procs.close();
        if (found.size() > 1) {
            throw new InvalidDataAccessApiUsageException("Unable to determine the correct call signature - multiple " + "procedures/functions/signatures for '" + metaDataProcedureName + "': found " + found);
        } else if (found.isEmpty()) {
            if (metaDataProcedureName.contains(".") && !StringUtils.hasText(metaDataCatalogName)) {
                String packageName = metaDataProcedureName.substring(0, metaDataProcedureName.indexOf("."));
                throw new InvalidDataAccessApiUsageException("Unable to determine the correct call signature for '" + metaDataProcedureName + "' - package name should be specified separately using '.withCatalogName(\"" + packageName + "\")'");
            } else {
                throw new InvalidDataAccessApiUsageException("Unable to determine the correct call signature - no " + "procedure/function/signature for '" + metaDataProcedureName + "'");
            }
        }
        procs = databaseMetaData.getProcedureColumns(metaDataCatalogName, metaDataSchemaName, metaDataProcedureName, null);
        while (procs.next()) {
            String columnName = procs.getString("COLUMN_NAME");
            int columnType = procs.getInt("COLUMN_TYPE");
            if (columnName == null && (columnType == DatabaseMetaData.procedureColumnIn || columnType == DatabaseMetaData.procedureColumnInOut || columnType == DatabaseMetaData.procedureColumnOut)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Skipping metadata for: " + columnType + " " + procs.getInt("DATA_TYPE") + " " + procs.getString("TYPE_NAME") + " " + procs.getInt("NULLABLE") + " (probably a member of a collection)");
                }
            } else {
                CallParameterMetaData meta = new CallParameterMetaData(columnName, columnType, procs.getInt("DATA_TYPE"), procs.getString("TYPE_NAME"), procs.getInt("NULLABLE") == DatabaseMetaData.procedureNullable);
                this.callParameterMetaData.add(meta);
                if (logger.isDebugEnabled()) {
                    logger.debug("Retrieved metadata: " + meta.getParameterName() + " " + meta.getParameterType() + " " + meta.getSqlType() + " " + meta.getTypeName() + " " + meta.isNullable());
                }
            }
        }
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error while retrieving metadata for procedure columns: " + ex);
        }
    } finally {
        try {
            if (procs != null) {
                procs.close();
            }
        } catch (SQLException ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Problem closing ResultSet for procedure column metadata: " + ex);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList)

Example 13 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-framework by spring-projects.

the class OracleTableMetaDataProvider method initializeWithTableColumnMetaData.

@Override
public void initializeWithTableColumnMetaData(DatabaseMetaData databaseMetaData, String catalogName, String schemaName, String tableName) throws SQLException {
    if (!this.includeSynonyms) {
        logger.debug("Defaulting to no synonyms in table metadata lookup");
        super.initializeWithTableColumnMetaData(databaseMetaData, catalogName, schemaName, tableName);
        return;
    }
    Connection con = databaseMetaData.getConnection();
    try {
        Class<?> oracleConClass = con.getClass().getClassLoader().loadClass("oracle.jdbc.OracleConnection");
        con = (Connection) con.unwrap(oracleConClass);
    } catch (ClassNotFoundException | SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Unable to include synonyms in table metadata lookup - no Oracle Connection: " + ex);
        }
        super.initializeWithTableColumnMetaData(databaseMetaData, catalogName, schemaName, tableName);
        return;
    }
    logger.debug("Including synonyms in table metadata lookup");
    Method setIncludeSynonyms;
    Boolean originalValueForIncludeSynonyms;
    try {
        Method getIncludeSynonyms = con.getClass().getMethod("getIncludeSynonyms", (Class[]) null);
        ReflectionUtils.makeAccessible(getIncludeSynonyms);
        originalValueForIncludeSynonyms = (Boolean) getIncludeSynonyms.invoke(con);
        setIncludeSynonyms = con.getClass().getMethod("setIncludeSynonyms", boolean.class);
        ReflectionUtils.makeAccessible(setIncludeSynonyms);
        setIncludeSynonyms.invoke(con, Boolean.TRUE);
    } catch (Throwable ex) {
        throw new InvalidDataAccessApiUsageException("Could not prepare Oracle Connection", ex);
    }
    super.initializeWithTableColumnMetaData(databaseMetaData, catalogName, schemaName, tableName);
    try {
        setIncludeSynonyms.invoke(con, originalValueForIncludeSynonyms);
    } catch (Throwable ex) {
        throw new InvalidDataAccessApiUsageException("Could not reset Oracle Connection", ex);
    }
}
Also used : SQLException(java.sql.SQLException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) Connection(java.sql.Connection) Method(java.lang.reflect.Method)

Example 14 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-framework by spring-projects.

the class RdbmsOperation method validateParameters.

/**
	 * Validate the parameters passed to an execute method based on declared parameters.
	 * Subclasses should invoke this method before every {@code executeQuery()}
	 * or {@code update()} method.
	 * @param parameters parameters supplied (may be {@code null})
	 * @throws InvalidDataAccessApiUsageException if the parameters are invalid
	 */
protected void validateParameters(Object[] parameters) throws InvalidDataAccessApiUsageException {
    checkCompiled();
    int declaredInParameters = 0;
    for (SqlParameter param : this.declaredParameters) {
        if (param.isInputValueProvided()) {
            if (!supportsLobParameters() && (param.getSqlType() == Types.BLOB || param.getSqlType() == Types.CLOB)) {
                throw new InvalidDataAccessApiUsageException("BLOB or CLOB parameters are not allowed for this kind of operation");
            }
            declaredInParameters++;
        }
    }
    validateParameterCount((parameters != null ? parameters.length : 0), declaredInParameters);
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Example 15 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-framework by spring-projects.

the class RdbmsOperation method validateNamedParameters.

/**
	 * Validate the named parameters passed to an execute method based on declared parameters.
	 * Subclasses should invoke this method before every {@code executeQuery()} or
	 * {@code update()} method.
	 * @param parameters parameter Map supplied. May be {@code null}.
	 * @throws InvalidDataAccessApiUsageException if the parameters are invalid
	 */
protected void validateNamedParameters(Map<String, ?> parameters) throws InvalidDataAccessApiUsageException {
    checkCompiled();
    Map<String, ?> paramsToUse = (parameters != null ? parameters : Collections.<String, Object>emptyMap());
    int declaredInParameters = 0;
    for (SqlParameter param : this.declaredParameters) {
        if (param.isInputValueProvided()) {
            if (!supportsLobParameters() && (param.getSqlType() == Types.BLOB || param.getSqlType() == Types.CLOB)) {
                throw new InvalidDataAccessApiUsageException("BLOB or CLOB parameters are not allowed for this kind of operation");
            }
            if (param.getName() != null && !paramsToUse.containsKey(param.getName())) {
                throw new InvalidDataAccessApiUsageException("The parameter named '" + param.getName() + "' was not among the parameters supplied: " + paramsToUse.keySet());
            }
            declaredInParameters++;
        }
    }
    validateParameterCount(paramsToUse.size(), declaredInParameters);
}
Also used : SqlParameter(org.springframework.jdbc.core.SqlParameter) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Aggregations

InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)18 Test (org.junit.Test)6 SQLException (java.sql.SQLException)5 SqlParameter (org.springframework.jdbc.core.SqlParameter)4 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 DataAccessException (org.springframework.dao.DataAccessException)3 PreparedStatement (java.sql.PreparedStatement)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 IncorrectResultSizeDataAccessException (org.springframework.dao.IncorrectResultSizeDataAccessException)2 MapPersistenceExceptionTranslator (org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1