Search in sources :

Example 46 with DataAccessResourceFailureException

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

the class OpenSessionInViewInterceptor method openSession.

/**
 * Open a Session for the SessionFactory that this interceptor uses.
 * <p>The default implementation delegates to the {@link SessionFactory#openSession}
 * method and sets the {@link Session}'s flush mode to "MANUAL".
 * @return the Session to use
 * @throws DataAccessResourceFailureException if the Session could not be created
 * @see FlushMode#MANUAL
 */
protected Session openSession() throws DataAccessResourceFailureException {
    try {
        Session session = obtainSessionFactory().openSession();
        session.setHibernateFlushMode(FlushMode.MANUAL);
        return session;
    } catch (HibernateException ex) {
        throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) Session(org.hibernate.Session)

Example 47 with DataAccessResourceFailureException

use of org.springframework.dao.DataAccessResourceFailureException 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);
    assertThat(divex.getCause()).isEqualTo(dataIntegrityViolationEx);
    SQLException featureNotSupEx = SQLExceptionSubclassFactory.newSQLFeatureNotSupportedException("", "", 0);
    InvalidDataAccessApiUsageException idaex = (InvalidDataAccessApiUsageException) sext.translate("task", "SQL", featureNotSupEx);
    assertThat(idaex.getCause()).isEqualTo(featureNotSupEx);
    SQLException dataIntegrityViolationEx2 = SQLExceptionSubclassFactory.newSQLIntegrityConstraintViolationException("", "", 0);
    DataIntegrityViolationException divex2 = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx2);
    assertThat(divex2.getCause()).isEqualTo(dataIntegrityViolationEx2);
    SQLException permissionDeniedEx = SQLExceptionSubclassFactory.newSQLInvalidAuthorizationSpecException("", "", 0);
    PermissionDeniedDataAccessException pdaex = (PermissionDeniedDataAccessException) sext.translate("task", "SQL", permissionDeniedEx);
    assertThat(pdaex.getCause()).isEqualTo(permissionDeniedEx);
    SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLNonTransientConnectionException("", "", 0);
    DataAccessResourceFailureException darex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataAccessResourceEx);
    assertThat(darex.getCause()).isEqualTo(dataAccessResourceEx);
    SQLException badSqlEx2 = SQLExceptionSubclassFactory.newSQLSyntaxErrorException("", "", 0);
    BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2);
    assertThat(bsgex2.getSql()).isEqualTo("SQL2");
    assertThat((Object) bsgex2.getSQLException()).isEqualTo(badSqlEx2);
    SQLException tranRollbackEx = SQLExceptionSubclassFactory.newSQLTransactionRollbackException("", "", 0);
    ConcurrencyFailureException cfex = (ConcurrencyFailureException) sext.translate("task", "SQL", tranRollbackEx);
    assertThat(cfex.getCause()).isEqualTo(tranRollbackEx);
    SQLException transientConnEx = SQLExceptionSubclassFactory.newSQLTransientConnectionException("", "", 0);
    TransientDataAccessResourceException tdarex = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx);
    assertThat(tdarex.getCause()).isEqualTo(transientConnEx);
    SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0);
    QueryTimeoutException tdarex2 = (QueryTimeoutException) sext.translate("task", "SQL", transientConnEx2);
    assertThat(tdarex2.getCause()).isEqualTo(transientConnEx2);
    SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0);
    RecoverableDataAccessException rdaex2 = (RecoverableDataAccessException) sext.translate("task", "SQL", recoverableEx);
    assertThat(rdaex2.getCause()).isEqualTo(recoverableEx);
    // 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);
    assertThat(bsgEct.getSql()).isEqualTo("SQL-ECT");
    assertThat((Object) bsgEct.getSQLException()).isEqualTo(sexEct);
    // 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);
    assertThat(bsgFbt.getSql()).isEqualTo("SQL-FBT");
    assertThat((Object) bsgFbt.getSQLException()).isEqualTo(sexFbt);
    // 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);
    assertThat(darfFbt.getCause()).isEqualTo(sexFbt2);
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) QueryTimeoutException(org.springframework.dao.QueryTimeoutException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) RecoverableDataAccessException(org.springframework.dao.RecoverableDataAccessException) Test(org.junit.jupiter.api.Test)

Example 48 with DataAccessResourceFailureException

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

the class AbstractSequenceMaxValueIncrementer method getNextKey.

/**
 * Executes the SQL as specified by {@link #getSequenceQuery()}.
 */
@Override
protected long getNextKey() throws DataAccessException {
    Connection con = DataSourceUtils.getConnection(getDataSource());
    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = con.createStatement();
        DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
        rs = stmt.executeQuery(getSequenceQuery());
        if (rs.next()) {
            return rs.getLong(1);
        } else {
            throw new DataAccessResourceFailureException("Sequence query did not return a result");
        }
    } catch (SQLException ex) {
        throw new DataAccessResourceFailureException("Could not obtain sequence value", ex);
    } finally {
        JdbcUtils.closeResultSet(rs);
        JdbcUtils.closeStatement(stmt);
        DataSourceUtils.releaseConnection(con, getDataSource());
    }
}
Also used : DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 49 with DataAccessResourceFailureException

use of org.springframework.dao.DataAccessResourceFailureException in project dhis2-core by dhis2.

the class JdbcAnalyticsManager method getAggregatedDataValues.

// -------------------------------------------------------------------------
// AnalyticsManager implementation
// -------------------------------------------------------------------------
@Override
@Async
public Future<Map<String, Object>> getAggregatedDataValues(DataQueryParams params, AnalyticsTableType tableType, int maxLimit) {
    assertQuery(params);
    try {
        ListMap<DimensionalItemObject, DimensionalItemObject> dataPeriodAggregationPeriodMap = params.getDataPeriodAggregationPeriodMap();
        if (params.isDisaggregation() && params.hasDataPeriodType()) {
            params = DataQueryParams.newBuilder(params).withDataPeriodsForAggregationPeriods(dataPeriodAggregationPeriodMap).build();
            params = queryPlanner.assignPartitionsFromQueryPeriods(params, tableType);
        }
        String sql = getSelectClause(params);
        sql += getFromClause(params);
        sql += getWhereClause(params, tableType);
        sql += getGroupByClause(params);
        if (params.hasMeasureCriteria() && params.isDataType(DataType.NUMERIC)) {
            sql += getMeasureCriteriaSql(params);
        }
        log.debug(sql);
        if (params.analyzeOnly()) {
            executionPlanStore.addExecutionPlan(params.getExplainOrderId(), sql);
            return new AsyncResult<>(Maps.newHashMap());
        }
        Map<String, Object> map;
        try {
            map = getKeyValueMap(params, sql, maxLimit);
        } catch (BadSqlGrammarException ex) {
            log.info(AnalyticsUtils.ERR_MSG_TABLE_NOT_EXISTING, ex);
            return new AsyncResult<>(Maps.newHashMap());
        }
        replaceDataPeriodsWithAggregationPeriods(map, params, dataPeriodAggregationPeriodMap);
        return new AsyncResult<>(map);
    } catch (DataAccessResourceFailureException ex) {
        log.warn(ErrorCode.E7131.getMessage(), ex);
        throw new QueryRuntimeException(ErrorCode.E7131, ex);
    } catch (RuntimeException ex) {
        log.error(DebugUtils.getStackTrace(ex));
        throw ex;
    }
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) QueryRuntimeException(org.hisp.dhis.common.QueryRuntimeException) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) QueryRuntimeException(org.hisp.dhis.common.QueryRuntimeException) DimensionalObject(org.hisp.dhis.common.DimensionalObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) AsyncResult(org.springframework.scheduling.annotation.AsyncResult) Async(org.springframework.scheduling.annotation.Async)

Example 50 with DataAccessResourceFailureException

use of org.springframework.dao.DataAccessResourceFailureException in project dhis2-core by dhis2.

the class JdbcEnrollmentAnalyticsManager method getEnrollmentCount.

@Override
public long getEnrollmentCount(EventQueryParams params) {
    String sql = "select count(pi) ";
    sql += getFromClause(params);
    sql += getWhereClause(params);
    long count = 0;
    try {
        log.debug("Analytics enrollment count SQL: " + sql);
        if (params.analyzeOnly()) {
            executionPlanStore.addExecutionPlan(params.getExplainOrderId(), sql);
        } else {
            count = jdbcTemplate.queryForObject(sql, Long.class);
        }
    } catch (BadSqlGrammarException ex) {
        log.info(AnalyticsUtils.ERR_MSG_TABLE_NOT_EXISTING, ex);
    } catch (DataAccessResourceFailureException ex) {
        log.warn(ErrorCode.E7131.getMessage(), ex);
        throw new QueryRuntimeException(ErrorCode.E7131, ex);
    }
    return count;
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) QueryRuntimeException(org.hisp.dhis.common.QueryRuntimeException) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString)

Aggregations

DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)53 SQLException (java.sql.SQLException)13 IOException (java.io.IOException)10 File (java.io.File)9 Connection (java.sql.Connection)9 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)6 HibernateException (org.hibernate.HibernateException)5 Session (org.hibernate.Session)5 InputStream (java.io.InputStream)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 StorageService (org.dkpro.lab.storage.StorageService)4 StorageKey (org.dkpro.lab.storage.StorageService.StorageKey)4 QueryRuntimeException (org.hisp.dhis.common.QueryRuntimeException)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 Statement (java.sql.Statement)3 InOrder (org.mockito.InOrder)3