Search in sources :

Example 36 with InvalidDataAccessApiUsageException

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

the class RdbmsOperation method compile.

/**
 * Compile this query.
 * Ignores subsequent attempts to compile.
 * @throws InvalidDataAccessApiUsageException if the object hasn't
 * been correctly initialized, for example if no DataSource has been provided
 */
public final void compile() throws InvalidDataAccessApiUsageException {
    if (!isCompiled()) {
        if (getSql() == null) {
            throw new InvalidDataAccessApiUsageException("Property 'sql' is required");
        }
        try {
            this.jdbcTemplate.afterPropertiesSet();
        } catch (IllegalArgumentException ex) {
            throw new InvalidDataAccessApiUsageException(ex.getMessage());
        }
        compileInternal();
        this.compiled = true;
        if (logger.isDebugEnabled()) {
            logger.debug("RdbmsOperation with SQL [" + getSql() + "] compiled");
        }
    }
}
Also used : InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Example 37 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(@Nullable 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)

Example 38 with InvalidDataAccessApiUsageException

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

the class EntityManagerFactoryUtilsTests method testTranslatesIllegalArgumentException.

@Test
public void testTranslatesIllegalArgumentException() {
    IllegalArgumentException iae = new IllegalArgumentException();
    DataAccessException dex = EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(iae);
    assertThat(dex.getCause()).isSameAs(iae);
    boolean condition = dex instanceof InvalidDataAccessApiUsageException;
    assertThat(condition).isTrue();
}
Also used : InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) DataAccessException(org.springframework.dao.DataAccessException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Test(org.junit.jupiter.api.Test)

Example 39 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project alfresco-repository by Alfresco.

the class TransactionServiceImplTest method testReadOnlyTxn.

@Test
public void testReadOnlyTxn() throws Exception {
    // start a read-only transaction
    transactionService.setAllowWrite(false, vetoName);
    UserTransaction txn = transactionService.getUserTransaction();
    txn.begin();
    // do some writing
    try {
        nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, getName() + "_" + System.currentTimeMillis());
        txn.commit();
        fail("Read-only transaction wasn't detected");
    } catch (ReadOnlyServerException e) {
        // This is now thrown at the lower layers, but it *is* possible for one of the later
        // exceptions to get through: Fixed ALF-3884: Share does not report access denied exceptions correctly
        @SuppressWarnings("unused") int i = 0;
    } catch (InvalidDataAccessApiUsageException e) {
        // expected this ...
        @SuppressWarnings("unused") int i = 0;
    } catch (TransientDataAccessResourceException e) {
        // or this - for MySQL (java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed.)
        @SuppressWarnings("unused") int i = 0;
    } catch (IllegalStateException e) {
        // or this - for MS SQLServer, Oracle (via AbstractNodeDAOImpl.getCurrentTransaction)
        @SuppressWarnings("unused") int i = 0;
    } catch (UncategorizedSQLException e) {
        // or this - for PostgreSQL (org.postgresql.util.PSQLException: ERROR: transaction is read-only)
        if (dialect instanceof PostgreSQLDialect) {
            // ALF-4226
            @SuppressWarnings("unused") int i = 0;
        } else {
            throw e;
        }
    } finally {
        transactionService.setAllowWrite(true, vetoName);
        try {
            txn.rollback();
        } catch (Throwable e) {
        }
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) PostgreSQLDialect(org.alfresco.repo.domain.dialect.PostgreSQLDialect) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ReadOnlyServerException(org.alfresco.service.transaction.ReadOnlyServerException) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Aggregations

InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)39 ArrayList (java.util.ArrayList)7 Test (org.junit.jupiter.api.Test)7 SQLException (java.sql.SQLException)6 List (java.util.List)6 Map (java.util.Map)6 Document (org.bson.Document)6 Collections (java.util.Collections)5 DataAccessException (org.springframework.dao.DataAccessException)5 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 Arrays (java.util.Arrays)4 SqlParameter (org.springframework.jdbc.core.SqlParameter)4 StringUtils (org.springframework.util.StringUtils)4 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)3 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 Statement (java.sql.Statement)2 DataSource (javax.sql.DataSource)2