Search in sources :

Example 31 with DataAccessException

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

the class SQLExceptionCustomTranslatorTests method dataAccessResourceException.

@Test
public void dataAccessResourceException() {
    SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 2);
    DataAccessException dae = sext.translate("task", "SQL", dataAccessResourceEx);
    assertEquals(dataAccessResourceEx, dae.getCause());
    assertThat(dae, instanceOf(TransientDataAccessResourceException.class));
}
Also used : TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) SQLException(java.sql.SQLException) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 32 with DataAccessException

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

the class SQLStateSQLExceptionTranslatorTests method doTest.

private void doTest(String sqlState, Class<?> dataAccessExceptionType) {
    SQLException ex = new SQLException(REASON, sqlState);
    SQLExceptionTranslator translator = new SQLStateSQLExceptionTranslator();
    DataAccessException dax = translator.translate(TASK, SQL, ex);
    assertNotNull("Translation must *never* result in a null DataAccessException being returned.", dax);
    assertEquals("Wrong DataAccessException type returned as the result of the translation", dataAccessExceptionType, dax.getClass());
    assertNotNull("The original SQLException must be preserved in the translated DataAccessException", dax.getCause());
    assertSame("The exact same original SQLException must be preserved in the translated DataAccessException", ex, dax.getCause());
}
Also used : SQLException(java.sql.SQLException) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) DataAccessException(org.springframework.dao.DataAccessException)

Example 33 with DataAccessException

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

the class JpaTransactionManager method doCommit.

@Override
protected void doCommit(DefaultTransactionStatus status) {
    JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
    if (status.isDebug()) {
        logger.debug("Committing JPA transaction on EntityManager [" + txObject.getEntityManagerHolder().getEntityManager() + "]");
    }
    try {
        EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
        tx.commit();
    } catch (RollbackException ex) {
        if (ex.getCause() instanceof RuntimeException) {
            DataAccessException dex = getJpaDialect().translateExceptionIfPossible((RuntimeException) ex.getCause());
            if (dex != null) {
                throw dex;
            }
        }
        throw new TransactionSystemException("Could not commit JPA transaction", ex);
    } catch (RuntimeException ex) {
        // Assumably failed to flush changes to database.
        throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect());
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) TransactionSystemException(org.springframework.transaction.TransactionSystemException) RollbackException(javax.persistence.RollbackException) DataAccessException(org.springframework.dao.DataAccessException)

Example 34 with DataAccessException

use of org.springframework.dao.DataAccessException in project opennms by OpenNMS.

the class DefaultEventConfDao method reloadConfig.

private synchronized void reloadConfig() throws DataAccessException {
    try {
        // Load the root event file
        Events events = JaxbUtils.unmarshal(Events.class, m_configResource);
        // Hash the list of event files for efficient lookup
        Set<String> eventFiles = new HashSet<>();
        eventFiles.addAll(events.getEventFiles());
        // if and only if they exist in the new root
        for (String eventFile : m_events.getEventFiles()) {
            if (!eventFiles.contains(eventFile)) {
                m_lastModifiedEventFiles.remove(eventFile);
                continue;
            }
            events.addLoadedEventFile(eventFile, m_events.getLoadEventsByFile(eventFile));
        }
        // Load/reload the event files as necessary
        events.loadEventFilesIfModified(m_configResource, m_lastModifiedEventFiles);
        // Order the events for efficient searching
        events.initialize(m_partition, new EventOrdering());
        m_events = events;
    } catch (Exception e) {
        throw new DataRetrievalFailureException("Unabled to load " + m_configResource, e);
    }
}
Also used : EventOrdering(org.opennms.netmgt.xml.eventconf.EventOrdering) Events(org.opennms.netmgt.xml.eventconf.Events) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) DataAccessException(org.springframework.dao.DataAccessException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 35 with DataAccessException

use of org.springframework.dao.DataAccessException in project opennms by OpenNMS.

the class MockEventWriter method process.

private void process(final Event event) throws EventProcessorException {
    LOG.debug("Writing event: {}", event);
    final OnmsEvent oe = new OnmsEvent();
    oe.setEventAutoAction((event.getAutoactionCount() > 0) ? AutoAction.format(event.getAutoaction(), EVENT_AUTOACTION_FIELD_SIZE) : null);
    oe.setEventCorrelation((event.getCorrelation() != null) ? org.opennms.netmgt.dao.util.Correlation.format(event.getCorrelation(), EVENT_CORRELATION_FIELD_SIZE) : null);
    oe.setEventCreateTime(event.getCreationTime());
    oe.setId(event.getDbid());
    oe.setEventDescr(event.getDescr());
    try {
        oe.setDistPoller(m_distPollerDao.get(event.getDistPoller()));
    } catch (final DataAccessException e) {
        throw new EventProcessorException(e);
    }
    oe.setEventHost(event.getHost());
    oe.setEventForward((event.getForwardCount() > 0) ? org.opennms.netmgt.dao.util.Forward.format(event.getForward(), EVENT_FORWARD_FIELD_SIZE) : null);
    oe.setIfIndex(event.getIfIndex());
    oe.setIpAddr(event.getInterfaceAddress());
    if (event.getLogmsg() != null) {
        // set log message
        oe.setEventLogMsg(EventDatabaseConstants.format(event.getLogmsg().getContent(), 0));
        final String logdest = event.getLogmsg().getDest();
        if (logdest.equals("logndisplay")) {
            // if 'logndisplay' set both log and display column to yes
            oe.setEventLog("Y");
            oe.setEventDisplay("Y");
        } else if (logdest.equals("logonly")) {
            // if 'logonly' set log column to true
            oe.setEventLog("Y");
            oe.setEventDisplay("N");
        } else if (logdest.equals("displayonly")) {
            // if 'displayonly' set display column to true
            oe.setEventLog("N");
            oe.setEventDisplay("Y");
        } else if (logdest.equals("suppress")) {
            // if 'suppress' set both log and display to false
            oe.setEventLog("N");
            oe.setEventDisplay("N");
        }
    }
    oe.setEventMouseOverText(event.getMouseovertext());
    try {
        oe.setNode(m_nodeDao.get(event.getNodeid().intValue()));
    } catch (final DataAccessException e) {
        throw new EventProcessorException(e);
    }
    if (event.getOperactionCount() > 0) {
        final List<Operaction> a = new ArrayList<>();
        final List<String> b = new ArrayList<>();
        for (final Operaction eoa : event.getOperactionCollection()) {
            a.add(eoa);
            b.add(eoa.getMenutext());
        }
        oe.setEventOperAction(OperatorAction.format(a, EVENT_OPERACTION_FIELD_SIZE));
        oe.setEventOperActionMenuText(EventDatabaseConstants.format(b, EVENT_OPERACTION_MENU_FIELD_SIZE));
    }
    oe.setEventOperInstruct(event.getOperinstruct());
    oe.setEventParametersFromEvent(event);
    oe.setEventPathOutage(event.getPathoutage());
    try {
        oe.setServiceType(m_serviceTypeDao.findByName(event.getService()));
    } catch (final DataAccessException e) {
        throw new EventProcessorException(e);
    }
    oe.setSeverityLabel(event.getSeverity());
    oe.setEventSnmp(SnmpInfo.format(event.getSnmp(), EVENT_SNMP_FIELD_SIZE));
    oe.setEventSnmpHost(EventDatabaseConstants.format(event.getSnmphost(), EVENT_SNMPHOST_FIELD_SIZE));
    oe.setEventSource(event.getSource());
    oe.setEventTime(event.getTime());
    if (event.getTticket() != null) {
        oe.setEventTTicket(EventDatabaseConstants.format(event.getTticket().getContent(), EVENT_TTICKET_FIELD_SIZE));
        oe.setEventTTicketState(event.getTticket().getState().equals("on") ? 1 : 0);
    }
    oe.setEventUei(event.getUei());
    m_eventDao.saveOrUpdate(oe);
}
Also used : EventProcessorException(org.opennms.netmgt.events.api.EventProcessorException) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Operaction(org.opennms.netmgt.xml.event.Operaction) ArrayList(java.util.ArrayList) DataAccessException(org.springframework.dao.DataAccessException)

Aggregations

DataAccessException (org.springframework.dao.DataAccessException)89 SQLException (java.sql.SQLException)40 Test (org.junit.Test)26 Connection (java.sql.Connection)17 ResultSet (java.sql.ResultSet)16 PreparedStatement (java.sql.PreparedStatement)14 MongoException (com.mongodb.MongoException)13 Document (org.bson.Document)8 TransactionStatus (org.springframework.transaction.TransactionStatus)7 HashMap (java.util.HashMap)6 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)5 DeadlockLoserDataAccessException (org.springframework.dao.DeadlockLoserDataAccessException)5 IOException (java.io.IOException)4 ConnectionCallback (org.springframework.jdbc.core.ConnectionCallback)4 SpringSqlParams (com.opengamma.elsql.SpringSqlParams)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 Statement (java.sql.Statement)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IJoinQueryString (org.apereo.portal.jdbc.IJoinQueryString)3