Search in sources :

Example 36 with DataAccessException

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

the class AlarmdIT method changeFields.

@Test
public void changeFields() throws InterruptedException, SQLException {
    assertEmptyAlarmTable();
    String reductionKey = "testUpdateField";
    MockNode node1 = m_mockNetwork.getNode(1);
    // Verify we have the default alarm
    sendNodeDownEvent(reductionKey, node1);
    int severity = m_jdbcTemplate.queryForObject("select severity from alarms a where a.reductionKey = ?", new Object[] { reductionKey }, Integer.class).intValue();
    assertEquals(OnmsSeverity.MAJOR, OnmsSeverity.get(severity));
    // Store the original logmsg from the original alarm (we are about to test changing it with subsequent alarm reduction)
    String defaultLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {

        @Override
        public String extractData(ResultSet results) throws SQLException, DataAccessException {
            results.next();
            int row = results.getRow();
            boolean isLast = results.isLast();
            boolean isFirst = results.isFirst();
            if (row != 1 && !isLast && !isFirst) {
                throw new SQLException("Row count is not = 1.  There should only be one row returned from the query: \n" + results.getStatement());
            }
            return results.getString(1);
        }
    });
    assertTrue("The logmsg column should not be null", defaultLogMsg != null);
    // Duplicate the alarm but change the severity and verify the change
    sendNodeDownEventWithUpdateFieldSeverity(reductionKey, node1, OnmsSeverity.CRITICAL);
    severity = m_jdbcTemplate.queryForObject("select severity from alarms", Integer.class).intValue();
    assertEquals("Severity should now be Critical", OnmsSeverity.CRITICAL, OnmsSeverity.get(severity));
    // Duplicate the alarm but don't force the change of severity
    sendNodeDownEvent(reductionKey, node1);
    severity = m_jdbcTemplate.queryForObject("select severity from alarms", Integer.class).intValue();
    assertEquals("Severity should still be Critical", OnmsSeverity.CRITICAL, OnmsSeverity.get(severity));
    // Duplicate the alarm and change the logmsg
    sendNodeDownEventChangeLogMsg(reductionKey, node1, "new logMsg");
    String newLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {

        @Override
        public String extractData(ResultSet results) throws SQLException, DataAccessException {
            results.next();
            return results.getString(1);
        }
    });
    assertEquals("new logMsg", newLogMsg);
    assertTrue(!newLogMsg.equals(defaultLogMsg));
    // Duplicate the alarm but force logmsg to not change (lggmsg field is updated by default)
    sendNodeDownEventDontChangeLogMsg(reductionKey, node1, "newer logMsg");
    newLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {

        @Override
        public String extractData(ResultSet results) throws SQLException, DataAccessException {
            results.next();
            return results.getString(1);
        }
    });
    assertTrue("The logMsg should not have changed.", !"newer logMsg".equals(newLogMsg));
    assertEquals("The logMsg should still be equal to the previous update.", "new logMsg", newLogMsg);
    // Duplicate the alarm with the default configuration and verify the logmsg has changed (as is the default behavior
    // for this field)
    sendNodeDownEvent(reductionKey, node1);
    newLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {

        @Override
        public String extractData(ResultSet results) throws SQLException, DataAccessException {
            results.next();
            return results.getString(1);
        }
    });
    assertTrue("The logMsg should have changed.", !"new logMsg".equals(newLogMsg));
    assertEquals("The logMsg should new be the default logMsg.", newLogMsg, defaultLogMsg);
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) MockNode(org.opennms.netmgt.mock.MockNode) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 37 with DataAccessException

use of org.springframework.dao.DataAccessException in project libresonic by Libresonic.

the class DBController method handleRequestInternal.

@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    String query = request.getParameter("query");
    if (query != null) {
        map.put("query", query);
        try {
            List<?> result = daoHelper.getJdbcTemplate().query(query, new ColumnMapRowMapper());
            map.put("result", result);
        } catch (DataAccessException x) {
            map.put("error", ExceptionUtils.getRootCause(x).getMessage());
        }
    }
    return new ModelAndView("db", "model", map);
}
Also used : HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) DataAccessException(org.springframework.dao.DataAccessException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 38 with DataAccessException

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

the class LocationMonitorIdValidator method validate.

/**
 * {@inheritDoc}
 */
@Override
public void validate(Object obj, Errors errors) {
    LocationMonitorIdCommand cmd = (LocationMonitorIdCommand) obj;
    if (cmd.getMonitorId() == null) {
        errors.rejectValue("monitorId", "monitorId.notSpecified", new Object[] { "monitorId" }, "Value required.");
    } else {
        try {
            String monitorId = cmd.getMonitorId();
            OnmsLocationMonitor monitor = m_locationMonitorDao.get(monitorId);
            if (monitor == null) {
                throw new ObjectRetrievalFailureException(OnmsLocationMonitor.class, monitorId, "Could not find location monitor with id " + monitorId, null);
            }
        } catch (DataAccessException e) {
            errors.rejectValue("monitorId", "monitorId.notFound", new Object[] { "monitorId", cmd.getMonitorId() }, "Valid location monitor ID required.");
        }
    }
}
Also used : LocationMonitorIdCommand(org.opennms.web.svclayer.model.LocationMonitorIdCommand) ObjectRetrievalFailureException(org.springframework.orm.ObjectRetrievalFailureException) OnmsLocationMonitor(org.opennms.netmgt.model.OnmsLocationMonitor) DataAccessException(org.springframework.dao.DataAccessException)

Example 39 with DataAccessException

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

the class StatisticsReportCommandValidator method validate.

/**
 * {@inheritDoc}
 */
@Override
public void validate(Object obj, Errors errors) {
    StatisticsReportCommand cmd = (StatisticsReportCommand) obj;
    if (cmd.getId() == null) {
        errors.rejectValue("id", "statisticsReportId.notSpecified", new Object[] { "id" }, "Value required.");
    } else {
        try {
            int id = cmd.getId();
            m_statisticsReportDao.load(id);
        } catch (DataAccessException e) {
            errors.rejectValue("id", "statisticsReportId.notFound", new Object[] { "id", cmd.getId() }, "Valid statistics report ID required.");
        }
    }
}
Also used : StatisticsReportCommand(org.opennms.web.svclayer.model.StatisticsReportCommand) DataAccessException(org.springframework.dao.DataAccessException)

Example 40 with DataAccessException

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

the class MongoExceptionTranslatorUnitTests method checkTranslatedMongoException.

private void checkTranslatedMongoException(Class<? extends Exception> clazz, int code) {
    DataAccessException translated = translator.translateExceptionIfPossible(new MongoException(code, ""));
    assertThat("Expected exception of type " + clazz.getName() + "!", translated, is(not(nullValue())));
    Throwable cause = translated.getRootCause();
    assertThat(cause, is(instanceOf(MongoException.class)));
    assertThat(((MongoException) cause).getCode(), is(code));
}
Also used : MongoException(com.mongodb.MongoException) 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