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));
}
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);
}
}
}
}
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);
}
}
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);
}
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);
}
Aggregations