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
*/
@SuppressWarnings("deprecation")
protected Session openSession() throws DataAccessResourceFailureException {
try {
Session session = getSessionFactory().openSession();
session.setFlushMode(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);
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());
}
use of org.springframework.dao.DataAccessResourceFailureException in project opennms by OpenNMS.
the class HeartbeatConsumer method handleMessage.
@Override
@Transactional
public void handleMessage(MinionIdentityDTO minionHandle) {
LOG.info("Received heartbeat for Minion with id: {} at location: {}", minionHandle.getId(), minionHandle.getLocation());
OnmsMinion minion = minionDao.findById(minionHandle.getId());
if (minion == null) {
minion = new OnmsMinion();
minion.setId(minionHandle.getId());
// The real location is filled in below, but we set this to null
// for now to detect requisition changes
minion.setLocation(null);
}
final String prevLocation = minion.getLocation();
final String nextLocation = minionHandle.getLocation();
minion.setLocation(minionHandle.getLocation());
// Provision the minions node before we alter the location
this.provision(minion, prevLocation, nextLocation);
if (minionHandle.getTimestamp() == null) {
// The heartbeat does not contain a timestamp - use the current time
minion.setLastUpdated(new Date());
LOG.info("Received heartbeat without a timestamp: {}", minionHandle);
} else if (minion.getLastUpdated() == null) {
// The heartbeat does contain a timestamp, and we don't have
// one set yet, so use whatever we've been given
minion.setLastUpdated(minionHandle.getTimestamp());
} else if (minionHandle.getTimestamp().after(minion.getLastUpdated())) {
// The timestamp in the heartbeat is more recent than the one we
// have stored, so update it
minion.setLastUpdated(minionHandle.getTimestamp());
} else {
// The timestamp in the heartbeat is earlier than the
// timestamp we have stored, so ignore it
LOG.info("Ignoring stale timestamp from heartbeat: {}", minionHandle);
}
minionDao.saveOrUpdate(minion);
if (prevLocation == null) {
final EventBuilder eventBuilder = new EventBuilder(EventConstants.MONITORING_SYSTEM_ADDED_UEI, "OpenNMS.Minion.Heartbeat");
eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_TYPE, OnmsMonitoringSystem.TYPE_MINION);
eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_ID, minionHandle.getId());
eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_LOCATION, nextLocation);
try {
eventProxy.send(eventBuilder.getEvent());
} catch (final EventProxyException e) {
throw new DataAccessResourceFailureException("Unable to send event", e);
}
} else if (!prevLocation.equals(nextLocation)) {
final EventBuilder eventBuilder = new EventBuilder(EventConstants.MONITORING_SYSTEM_LOCATION_CHANGED_UEI, "OpenNMS.Minion.Heartbeat");
eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_TYPE, OnmsMonitoringSystem.TYPE_MINION);
eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_ID, minionHandle.getId());
eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_PREV_LOCATION, prevLocation);
eventBuilder.addParam(EventConstants.PARAM_MONITORING_SYSTEM_LOCATION, nextLocation);
try {
eventProxy.send(eventBuilder.getEvent());
} catch (final EventProxyException e) {
throw new DataAccessResourceFailureException("Unable to send event", e);
}
}
}
use of org.springframework.dao.DataAccessResourceFailureException in project opennms by OpenNMS.
the class DefaultPollerSettings method save.
private void save() {
FileOutputStream out = null;
try {
File configFile = m_configResource.getFile();
if (!configFile.getParentFile().exists()) {
if (!configFile.getParentFile().mkdirs()) {
LOG.warn("Could not make directory: {}", configFile.getPath());
}
}
out = new FileOutputStream(configFile);
m_settings.store(out, "Properties File for OpenNMS Remote Poller");
} catch (IOException e) {
throw new DataAccessResourceFailureException("Unable to save properties to " + m_configResource, e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
;
}
}
}
use of org.springframework.dao.DataAccessResourceFailureException in project opennms by OpenNMS.
the class Events method saveEvents.
public void saveEvents(final Resource resource) {
final StringWriter stringWriter = new StringWriter();
JaxbUtils.marshal(this, stringWriter);
if (stringWriter.toString() != null) {
File file;
try {
file = resource.getFile();
} catch (final IOException e) {
throw new DataAccessResourceFailureException("Event resource '" + resource + "' is not a file resource and cannot be saved. Nested exception: " + e, e);
}
try (final OutputStream fos = new FileOutputStream(file);
final Writer fileWriter = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
fileWriter.write(stringWriter.toString());
} catch (final Exception e) {
throw new DataAccessResourceFailureException("Event file '" + file + "' could not be opened. Nested exception: " + e, e);
}
}
}
Aggregations