Search in sources :

Example 1 with SqlExecutionException

use of org.seasar.doma.jdbc.SqlExecutionException 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 2 with SqlExecutionException

use of org.seasar.doma.jdbc.SqlExecutionException in project doma by domaframework.

the class SelectCommand method execute.

@Override
public RESULT execute() {
    Supplier<RESULT> supplier = null;
    Connection connection = JdbcUtil.getConnection(query.getConfig().getDataSource());
    try {
        PreparedStatement preparedStatement = JdbcUtil.prepareStatement(connection, sql);
        try {
            log();
            setupOptions(preparedStatement);
            bindParameters(preparedStatement);
            supplier = executeQuery(preparedStatement);
        } catch (SQLException e) {
            Dialect dialect = query.getConfig().getDialect();
            throw new SqlExecutionException(query.getConfig().getExceptionSqlLogType(), sql, e, dialect.getRootCause(e));
        } finally {
            close(supplier, () -> JdbcUtil.close(preparedStatement, query.getConfig().getJdbcLogger()));
        }
    } finally {
        close(supplier, () -> JdbcUtil.close(connection, query.getConfig().getJdbcLogger()));
    }
    return supplier.get();
}
Also used : SqlExecutionException(org.seasar.doma.jdbc.SqlExecutionException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Dialect(org.seasar.doma.jdbc.dialect.Dialect) PreparedStatement(java.sql.PreparedStatement)

Example 3 with SqlExecutionException

use of org.seasar.doma.jdbc.SqlExecutionException in project doma by domaframework.

the class ModifyCommand method execute.

@Override
public Integer execute() {
    if (!query.isExecutable()) {
        JdbcLogger logger = query.getConfig().getJdbcLogger();
        logger.logSqlExecutionSkipping(query.getClassName(), query.getMethodName(), query.getSqlExecutionSkipCause());
        return 0;
    }
    Connection connection = JdbcUtil.getConnection(query.getConfig().getDataSource());
    try {
        PreparedStatement preparedStatement = prepareStatement(connection);
        try {
            log();
            setupOptions(preparedStatement);
            bindParameters(preparedStatement);
            return executeInternal(preparedStatement);
        } catch (SQLException e) {
            Dialect dialect = query.getConfig().getDialect();
            throw new SqlExecutionException(query.getConfig().getExceptionSqlLogType(), sql, e, dialect.getRootCause(e));
        } finally {
            JdbcUtil.close(preparedStatement, query.getConfig().getJdbcLogger());
        }
    } finally {
        JdbcUtil.close(connection, query.getConfig().getJdbcLogger());
    }
}
Also used : SqlExecutionException(org.seasar.doma.jdbc.SqlExecutionException) JdbcLogger(org.seasar.doma.jdbc.JdbcLogger) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Dialect(org.seasar.doma.jdbc.dialect.Dialect) PreparedStatement(java.sql.PreparedStatement)

Example 4 with SqlExecutionException

use of org.seasar.doma.jdbc.SqlExecutionException in project doma by domaframework.

the class ModuleCommand method execute.

@Override
public RESULT execute() {
    Connection connection = JdbcUtil.getConnection(query.getConfig().getDataSource());
    try {
        CallableStatement callableStatement = JdbcUtil.prepareCall(connection, sql);
        try {
            log();
            setupOptions(callableStatement);
            bindParameters(callableStatement);
            return executeInternal(callableStatement);
        } catch (SQLException e) {
            Dialect dialect = query.getConfig().getDialect();
            throw new SqlExecutionException(query.getConfig().getExceptionSqlLogType(), sql, e, dialect.getRootCause(e));
        } finally {
            JdbcUtil.close(callableStatement, query.getConfig().getJdbcLogger());
        }
    } finally {
        JdbcUtil.close(connection, query.getConfig().getJdbcLogger());
    }
}
Also used : SqlExecutionException(org.seasar.doma.jdbc.SqlExecutionException) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) Dialect(org.seasar.doma.jdbc.dialect.Dialect)

Aggregations

SQLException (java.sql.SQLException)4 SqlExecutionException (org.seasar.doma.jdbc.SqlExecutionException)4 Connection (java.sql.Connection)3 Dialect (org.seasar.doma.jdbc.dialect.Dialect)3 PreparedStatement (java.sql.PreparedStatement)2 CallableStatement (java.sql.CallableStatement)1 JdbcException (org.seasar.doma.jdbc.JdbcException)1 JdbcLogger (org.seasar.doma.jdbc.JdbcLogger)1 NoResultException (org.seasar.doma.jdbc.NoResultException)1 NonSingleColumnException (org.seasar.doma.jdbc.NonSingleColumnException)1 NonUniqueResultException (org.seasar.doma.jdbc.NonUniqueResultException)1 OptimisticLockException (org.seasar.doma.jdbc.OptimisticLockException)1 ResultMappingException (org.seasar.doma.jdbc.ResultMappingException)1 UniqueConstraintException (org.seasar.doma.jdbc.UniqueConstraintException)1 UnknownColumnException (org.seasar.doma.jdbc.UnknownColumnException)1 DataAccessException (org.springframework.dao.DataAccessException)1 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)1 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)1 IncorrectResultSizeDataAccessException (org.springframework.dao.IncorrectResultSizeDataAccessException)1 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)1