Search in sources :

Example 31 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
	 */
@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);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) Session(org.hibernate.Session)

Example 32 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);
    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());
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) QueryTimeoutException(org.springframework.dao.QueryTimeoutException) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) RecoverableDataAccessException(org.springframework.dao.RecoverableDataAccessException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 33 with DataAccessResourceFailureException

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);
        }
    }
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) OnmsMinion(org.opennms.netmgt.model.minion.OnmsMinion) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 34 with DataAccessResourceFailureException

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) {
            }
            ;
        }
    }
}
Also used : DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 35 with DataAccessResourceFailureException

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);
        }
    }
}
Also used : StringWriter(java.io.StringWriter) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException) IOException(java.io.IOException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException)

Aggregations

DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)36 SQLException (java.sql.SQLException)10 Connection (java.sql.Connection)6 IOException (java.io.IOException)5 HibernateException (org.hibernate.HibernateException)4 Session (org.hibernate.Session)4 File (java.io.File)3 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 Test (org.junit.Test)3 EventProxyException (org.opennms.netmgt.events.api.EventProxyException)3 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)3 FileOutputStream (java.io.FileOutputStream)2 Clob (java.sql.Clob)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 Date (java.util.Date)2 EntityManager (javax.persistence.EntityManager)2 PersistenceException (javax.persistence.PersistenceException)2 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)2 DatabaseMetaDataCallback (org.springframework.jdbc.support.DatabaseMetaDataCallback)2