Search in sources :

Example 21 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 22 with InvalidDataAccessApiUsageException

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

the class AbstractJdbcCall method compile.

//-------------------------------------------------------------------------
// Methods handling compilation issues
//-------------------------------------------------------------------------
/**
	 * Compile this JdbcCall using provided parameters and meta data plus other settings.
	 * <p>This finalizes the configuration for this object and subsequent attempts to compile are
	 * ignored. This will be implicitly called the first time an un-compiled call is executed.
	 * @throws org.springframework.dao.InvalidDataAccessApiUsageException if the object hasn't
	 * been correctly initialized, for example if no DataSource has been provided
	 */
public final synchronized void compile() throws InvalidDataAccessApiUsageException {
    if (!isCompiled()) {
        if (getProcedureName() == null) {
            throw new InvalidDataAccessApiUsageException("Procedure or Function name is required");
        }
        try {
            this.jdbcTemplate.afterPropertiesSet();
        } catch (IllegalArgumentException ex) {
            throw new InvalidDataAccessApiUsageException(ex.getMessage());
        }
        compileInternal();
        this.compiled = true;
        if (logger.isDebugEnabled()) {
            logger.debug("SqlCall for " + (isFunction() ? "function" : "procedure") + " [" + getProcedureName() + "] compiled");
        }
    }
}
Also used : InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Example 23 with InvalidDataAccessApiUsageException

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

the class SQLExceptionSubclassTranslatorTests method errorCodeTranslation.

@Test
public void errorCodeTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 0);
    DataIntegrityViolationException divex = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx);
    assertEquals(dataIntegrityViolationEx, divex.getCause());
    SQLException featureNotSupEx = SQLExceptionSubclassFactory.newSQLFeatureNotSupportedException("", "", 0);
    InvalidDataAccessApiUsageException idaex = (InvalidDataAccessApiUsageException) sext.translate("task", "SQL", featureNotSupEx);
    assertEquals(featureNotSupEx, idaex.getCause());
    SQLException dataIntegrityViolationEx2 = SQLExceptionSubclassFactory.newSQLIntegrityConstraintViolationException("", "", 0);
    DataIntegrityViolationException divex2 = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx2);
    assertEquals(dataIntegrityViolationEx2, divex2.getCause());
    SQLException permissionDeniedEx = SQLExceptionSubclassFactory.newSQLInvalidAuthorizationSpecException("", "", 0);
    PermissionDeniedDataAccessException pdaex = (PermissionDeniedDataAccessException) sext.translate("task", "SQL", permissionDeniedEx);
    assertEquals(permissionDeniedEx, pdaex.getCause());
    SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLNonTransientConnectionException("", "", 0);
    DataAccessResourceFailureException darex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataAccessResourceEx);
    assertEquals(dataAccessResourceEx, darex.getCause());
    SQLException badSqlEx2 = SQLExceptionSubclassFactory.newSQLSyntaxErrorException("", "", 0);
    BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2);
    assertEquals("SQL2", bsgex2.getSql());
    assertEquals(badSqlEx2, bsgex2.getSQLException());
    SQLException tranRollbackEx = SQLExceptionSubclassFactory.newSQLTransactionRollbackException("", "", 0);
    ConcurrencyFailureException cfex = (ConcurrencyFailureException) sext.translate("task", "SQL", tranRollbackEx);
    assertEquals(tranRollbackEx, cfex.getCause());
    SQLException transientConnEx = SQLExceptionSubclassFactory.newSQLTransientConnectionException("", "", 0);
    TransientDataAccessResourceException tdarex = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx);
    assertEquals(transientConnEx, tdarex.getCause());
    SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0);
    QueryTimeoutException tdarex2 = (QueryTimeoutException) sext.translate("task", "SQL", transientConnEx2);
    assertEquals(transientConnEx2, tdarex2.getCause());
    SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0);
    RecoverableDataAccessException rdaex2 = (RecoverableDataAccessException) sext.translate("task", "SQL", recoverableEx);
    assertEquals(recoverableEx, rdaex2.getCause());
    // Test classic error code translation. We should move there next if the exception we pass in is not one
    // of the new sub-classes.
    SQLException sexEct = new SQLException("", "", 1);
    BadSqlGrammarException bsgEct = (BadSqlGrammarException) sext.translate("task", "SQL-ECT", sexEct);
    assertEquals("SQL-ECT", bsgEct.getSql());
    assertEquals(sexEct, bsgEct.getSQLException());
    // Test fallback. We assume that no database will ever return this error code,
    // but 07xxx will be bad grammar picked up by the fallback SQLState translator
    SQLException sexFbt = new SQLException("", "07xxx", 666666666);
    BadSqlGrammarException bsgFbt = (BadSqlGrammarException) sext.translate("task", "SQL-FBT", sexFbt);
    assertEquals("SQL-FBT", bsgFbt.getSql());
    assertEquals(sexFbt, bsgFbt.getSQLException());
    // and 08xxx will be data resource failure (non-transient) picked up by the fallback SQLState translator
    SQLException sexFbt2 = new SQLException("", "08xxx", 666666666);
    DataAccessResourceFailureException darfFbt = (DataAccessResourceFailureException) sext.translate("task", "SQL-FBT2", sexFbt2);
    assertEquals(sexFbt2, darfFbt.getCause());
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) QueryTimeoutException(org.springframework.dao.QueryTimeoutException) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) RecoverableDataAccessException(org.springframework.dao.RecoverableDataAccessException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 24 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);
    assertSame(iae, dex.getCause());
    assertTrue(dex instanceof InvalidDataAccessApiUsageException);
}
Also used : InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) DataAccessException(org.springframework.dao.DataAccessException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Test(org.junit.Test)

Example 25 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project leopard by tanhaichao.

the class JdbcMysqlImpl method insert.

@Override
public boolean insert(String tableName, Object bean) {
    InsertBuilder builder = new InsertBuilder(tableName);
    Field[] fields = bean.getClass().getDeclaredFields();
    for (Field field : fields) {
        String fieldName = field.getName();
        Class<?> type = field.getType();
        field.setAccessible(true);
        Object obj;
        try {
            obj = field.get(bean);
        }// }
         catch (IllegalAccessException e) {
            throw new InvalidDataAccessApiUsageException(e.getMessage());
        }
        if (String.class.equals(type)) {
            builder.setString(fieldName, (String) obj);
        } else if (boolean.class.equals(type) || Boolean.class.equals(type)) {
            builder.setBool(fieldName, (Boolean) obj);
        } else if (int.class.equals(type) || Integer.class.equals(type)) {
            builder.setInt(fieldName, (Integer) obj);
        } else if (long.class.equals(type) || Long.class.equals(type)) {
            builder.setLong(fieldName, (Long) obj);
        } else if (float.class.equals(type) || Float.class.equals(type)) {
            builder.setFloat(fieldName, (Float) obj);
        } else if (double.class.equals(type) || Double.class.equals(type)) {
            builder.setDouble(fieldName, (Double) obj);
        } else if (Date.class.equals(type)) {
            builder.setDate(fieldName, (Date) obj);
        } else if (List.class.equals(type)) {
            builder.setString(fieldName, obj.toString());
        } else {
            throw new InvalidDataAccessApiUsageException("未知数据类型[" + type.getName() + "].");
        }
    }
    return this.insertForBoolean(builder);
}
Also used : InsertBuilder(io.leopard.jdbc.builder.InsertBuilder) Field(java.lang.reflect.Field) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) List(java.util.List)

Aggregations

InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)29 Document (org.bson.Document)6 Test (org.junit.Test)6 SQLException (java.sql.SQLException)5 DataAccessException (org.springframework.dao.DataAccessException)5 SqlParameter (org.springframework.jdbc.core.SqlParameter)4 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)3 Filters (com.mongodb.client.model.Filters)2 ReturnDocument (com.mongodb.client.model.ReturnDocument)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 Method (java.lang.reflect.Method)2 PreparedStatement (java.sql.PreparedStatement)2 HashMap (java.util.HashMap)2 List (java.util.List)2 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)2