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);
}
}
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);
}
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());
}
}
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;
}
}
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;
}
Aggregations