Search in sources :

Example 11 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project doma-spring-boot by domaframework.

the class DomaPersistenceExceptionTranslator method translateExceptionIfPossible.

@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
    if (!(ex instanceof JdbcException)) {
        // Fallback to other translators if not JdbcException
        return null;
    }
    if (ex instanceof OptimisticLockException) {
        return new OptimisticLockingFailureException(ex.getMessage(), ex);
    } else if (ex instanceof UniqueConstraintException) {
        return new DuplicateKeyException(ex.getMessage(), ex);
    } else if (ex instanceof NonUniqueResultException || ex instanceof NonSingleColumnException) {
        return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
    } else if (ex instanceof NoResultException) {
        return new EmptyResultDataAccessException(ex.getMessage(), 1, ex);
    } else if (ex instanceof UnknownColumnException || ex instanceof ResultMappingException) {
        return new TypeMismatchDataAccessException(ex.getMessage(), ex);
    }
    if (ex.getCause() instanceof SQLException) {
        SQLException e = (SQLException) ex.getCause();
        String sql = null;
        if (ex instanceof SqlExecutionException) {
            sql = ((SqlExecutionException) ex).getRawSql();
        }
        DataAccessException dae = translator.translate(ex.getMessage(), sql, e);
        return (dae != null ? dae : new UncategorizedSQLException(ex.getMessage(), sql, e));
    }
    return new UncategorizedDataAccessException(ex.getMessage(), ex) {
    };
}
Also used : NonUniqueResultException(org.seasar.doma.jdbc.NonUniqueResultException) NonSingleColumnException(org.seasar.doma.jdbc.NonSingleColumnException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) OptimisticLockException(org.seasar.doma.jdbc.OptimisticLockException) NoResultException(org.seasar.doma.jdbc.NoResultException) ResultMappingException(org.seasar.doma.jdbc.ResultMappingException) JdbcException(org.seasar.doma.jdbc.JdbcException) TypeMismatchDataAccessException(org.springframework.dao.TypeMismatchDataAccessException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) UncategorizedDataAccessException(org.springframework.dao.UncategorizedDataAccessException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) UnknownColumnException(org.seasar.doma.jdbc.UnknownColumnException) SqlExecutionException(org.seasar.doma.jdbc.SqlExecutionException) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) UniqueConstraintException(org.seasar.doma.jdbc.UniqueConstraintException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) DataAccessException(org.springframework.dao.DataAccessException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) TypeMismatchDataAccessException(org.springframework.dao.TypeMismatchDataAccessException) UncategorizedDataAccessException(org.springframework.dao.UncategorizedDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 12 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project com.revolsys.open by revolsys.

the class JdbcConnection method getException.

public DataAccessException getException(final String task, final String sql, final SQLException e) {
    SQLExceptionTranslator exceptionTransaltor;
    if (this.dataSource == null) {
        exceptionTransaltor = new SQLStateSQLExceptionTranslator();
    } else {
        exceptionTransaltor = new SQLErrorCodeSQLExceptionTranslator(this.dataSource);
    }
    final DataAccessException translatedException = exceptionTransaltor.translate(task, sql, e);
    if (translatedException == null) {
        return new UncategorizedSQLException(task, sql, e);
    } else {
        return translatedException;
    }
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) SQLExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator) SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator) DataAccessException(org.springframework.dao.DataAccessException)

Example 13 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project thinglinks by mqttsnet.

the class TdEngineController method addColumnForSuperTable.

@PostMapping("/addColumnInStb")
public R addColumnForSuperTable(@RequestBody SuperTableDto superTableDto) {
    String superTableName = superTableDto.getSuperTableName();
    if (StringUtils.isBlank(superTableName)) {
        return R.fail("invalid operation: superTableName can not be empty");
    }
    Fields fields = superTableDto.getFields();
    if (fields == null) {
        return R.fail("invalid operation: fields can not be empty");
    }
    try {
        FieldsVo fieldsVo = FieldsVo.fieldsTranscoding(fields);
        this.tdEngineService.addColumnForSuperTable(superTableName, fieldsVo);
        log.info("successful operation: add column for superTable '" + superTableName + "' success");
        return R.ok();
    } catch (UncategorizedSQLException e) {
        String message = e.getCause().getMessage();
        try {
            message = message.substring(message.lastIndexOf("invalid operation"));
        } catch (Exception ex) {
        }
        log.error(message);
        return R.fail(message);
    } catch (SQLException e) {
        log.error(e.getMessage());
        return R.fail(e.getMessage());
    }
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) Fields(com.mqttsnet.thinglinks.tdengine.api.domain.Fields) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) FieldsVo(com.mqttsnet.thinglinks.tdengine.api.domain.FieldsVo) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException)

Example 14 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project thinglinks by mqttsnet.

the class TdEngineController method createSuperTable.

/**
 * @param superTableDto 创建超级表需要的入参的实体类
 * @return R
 * @MethodDescription 创建超级表
 * @author thinglinks
 * @Date 2021/12/27 16:26
 */
@PostMapping("/createSTb")
public R createSuperTable(@Validated @RequestBody SuperTableDto superTableDto) {
    // 从入参对象获取列字段(超级表结构)对象集合
    List<Fields> schemaFields = superTableDto.getSchemaFields();
    // 从入参对象获取标签字段对象集合
    List<Fields> tagsFields = superTableDto.getTagsFields();
    // 从入参获取数据库名称
    String databaseName = superTableDto.getDatabaseName();
    // 从入参获取超级表名称
    String superTableName = superTableDto.getSuperTableName();
    // 获取列字段对象集合的第一个对象的字段数据类型
    DataTypeEnum dataType = schemaFields.get(0).getDataType();
    // 如果该数据类型不是时间戳,打印和返回报错信息
    if (dataType == null || !"timestamp".equals(dataType.getDataType())) {
        log.error("invalid operation: first column must be timestamp");
        return R.fail("invalid operation: the first column must be timestamp");
    }
    try {
        // 将列字段对象集合和标签字段对象集合转码为字段Vo类对象集合
        List<FieldsVo> schemaFieldsVoList = FieldsVo.fieldsTranscoding(schemaFields);
        List<FieldsVo> tagsFieldsVoList = FieldsVo.fieldsTranscoding(tagsFields);
        // 创建超级表
        this.tdEngineService.createSuperTable(schemaFieldsVoList, tagsFieldsVoList, databaseName, superTableName);
        log.info("successful operation: created superTable '" + superTableName + "' success");
        return R.ok();
    } catch (UncategorizedSQLException e) {
        String message = e.getCause().getMessage();
        try {
            message = message.substring(message.lastIndexOf("invalid operation"));
        } catch (Exception ex) {
        }
        log.error(message);
        return R.fail(message);
    } catch (SQLException e) {
        log.error(e.getMessage());
        return R.fail(e.getMessage());
    }
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) Fields(com.mqttsnet.thinglinks.tdengine.api.domain.Fields) DataTypeEnum(com.mqttsnet.thinglinks.common.core.enums.DataTypeEnum) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) FieldsVo(com.mqttsnet.thinglinks.tdengine.api.domain.FieldsVo) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException)

Example 15 with UncategorizedSQLException

use of org.springframework.jdbc.UncategorizedSQLException in project solarnetwork-central by SolarNetwork.

the class MyBatisExceptionTranslator method translateExceptionIfPossible.

@Override
public DataAccessException translateExceptionIfPossible(RuntimeException e) {
    if (e != null && e.getCause() instanceof DataAccessException) {
        // could be something like CannotGetJdbcConnectionException, so use that directly
        return (DataAccessException) e.getCause();
    }
    DataAccessException result = super.translateExceptionIfPossible(e);
    if (result instanceof UncategorizedSQLException || result == null) {
        SQLException sqlEx = null;
        Throwable t = e;
        while (t != null && sqlEx == null) {
            if (t instanceof SQLException) {
                sqlEx = (SQLException) t;
            } else if (t instanceof UncategorizedSQLException) {
                sqlEx = ((UncategorizedSQLException) t).getSQLException();
            }
            t = t.getCause();
        }
        if (sqlEx != null) {
            final String sqlState = sqlEx.getSQLState();
            final String sqlMessage = sqlEx.getMessage();
            if (sqlStateConfigurations == null) {
                loadExceptionProperties();
            }
            final List<SqlStateErrorConfig> configs = (sqlStateConfigurations != null ? sqlStateConfigurations.get(sqlState) : null);
            if (configs != null) {
                for (SqlStateErrorConfig config : configs) {
                    if (config.messagePattern.matcher(sqlMessage).find()) {
                        result = config.type.toException(sqlEx, result);
                    }
                }
            }
        }
    }
    return result;
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) DataAccessException(org.springframework.dao.DataAccessException)

Aggregations

UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)37 SQLException (java.sql.SQLException)28 InOrder (org.mockito.InOrder)20 TransactionStatus (org.springframework.transaction.TransactionStatus)20 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)20 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)20 Test (org.junit.jupiter.api.Test)15 Connection (java.sql.Connection)12 DataSource (javax.sql.DataSource)8 DataAccessException (org.springframework.dao.DataAccessException)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Fields (com.mqttsnet.thinglinks.tdengine.api.domain.Fields)3 BatchUpdateException (java.sql.BatchUpdateException)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 TransactionAwareDataSourceProxy (org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy)3 FieldsVo (com.mqttsnet.thinglinks.tdengine.api.domain.FieldsVo)2 Test (org.junit.Test)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 ConnectionProxy (org.springframework.jdbc.datasource.ConnectionProxy)2 LazyConnectionDataSourceProxy (org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy)2