Search in sources :

Example 1 with DataAccessResourceFailureException

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

the class DefaultRequisitionAccessService method importRequisition.

@Override
public void importRequisition(final String foreignSource, final String rescanExisting) {
    final URL activeUrl = createSnapshot(foreignSource);
    final String url = activeUrl.toString();
    LOG.debug("importRequisition: Sending import event with URL {}", url);
    final EventBuilder bldr = new EventBuilder(EventConstants.RELOAD_IMPORT_UEI, "Web");
    bldr.addParam(EventConstants.PARM_URL, url);
    if (rescanExisting != null) {
        bldr.addParam(EventConstants.PARM_IMPORT_RESCAN_EXISTING, rescanExisting);
    }
    try {
        getEventProxy().send(bldr.getEvent());
    } catch (final EventProxyException e) {
        throw new DataAccessResourceFailureException("Unable to send event to import group " + foreignSource, e);
    }
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) URL(java.net.URL)

Example 2 with DataAccessResourceFailureException

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

the class DefaultRrdDaoTest method testPrintValueWithBogusLine.

public void testPrintValueWithBogusLine() throws Exception {
    long end = System.currentTimeMillis();
    long start = end - (24 * 60 * 60 * 1000);
    String printLine = "blah blah blah this should be a floating point number blah blah blah";
    OnmsResource childResource = preparePrintValueTest(start, end, printLine);
    m_mocks.replayAll();
    ThrowableAnticipator ta = new ThrowableAnticipator();
    ta.anticipate(new DataAccessResourceFailureException("Value of line 1 of output from RRD is not a valid floating point number: '" + printLine + "'"));
    try {
        m_dao.getPrintValue(childResource.getAttributes().iterator().next(), "AVERAGE", start, end);
    } catch (Throwable t) {
        ta.throwableReceived(t);
    }
    m_mocks.verifyAll();
    ta.verifyAnticipated();
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) ThrowableAnticipator(org.opennms.test.ThrowableAnticipator)

Example 3 with DataAccessResourceFailureException

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

the class CciTemplate method execute.

@Override
public <T> T execute(ConnectionCallback<T> action) throws DataAccessException {
    Assert.notNull(action, "Callback object must not be null");
    Connection con = ConnectionFactoryUtils.getConnection(getConnectionFactory(), getConnectionSpec());
    try {
        return action.doInConnection(con, getConnectionFactory());
    } catch (NotSupportedException ex) {
        throw new CciOperationNotSupportedException("CCI operation not supported by connector", ex);
    } catch (ResourceException ex) {
        throw new DataAccessResourceFailureException("CCI operation failed", ex);
    } catch (SQLException ex) {
        throw new InvalidResultSetAccessException("Parsing of CCI ResultSet failed", ex);
    } finally {
        ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory());
    }
}
Also used : CciOperationNotSupportedException(org.springframework.jca.cci.CciOperationNotSupportedException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) SQLException(java.sql.SQLException) Connection(javax.resource.cci.Connection) ResourceException(javax.resource.ResourceException) InvalidResultSetAccessException(org.springframework.jca.cci.InvalidResultSetAccessException) RecordTypeNotSupportedException(org.springframework.jca.cci.RecordTypeNotSupportedException) NotSupportedException(javax.resource.NotSupportedException) CciOperationNotSupportedException(org.springframework.jca.cci.CciOperationNotSupportedException)

Example 4 with DataAccessResourceFailureException

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

the class SQLErrorCodeSQLExceptionTranslator method doTranslate.

@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
    SQLException sqlEx = ex;
    if (sqlEx instanceof BatchUpdateException && sqlEx.getNextException() != null) {
        SQLException nestedSqlEx = sqlEx.getNextException();
        if (nestedSqlEx.getErrorCode() > 0 || nestedSqlEx.getSQLState() != null) {
            logger.debug("Using nested SQLException from the BatchUpdateException");
            sqlEx = nestedSqlEx;
        }
    }
    // First, try custom translation from overridden method.
    DataAccessException dex = customTranslate(task, sql, sqlEx);
    if (dex != null) {
        return dex;
    }
    // Next, try the custom SQLException translator, if available.
    if (this.sqlErrorCodes != null) {
        SQLExceptionTranslator customTranslator = this.sqlErrorCodes.getCustomSqlExceptionTranslator();
        if (customTranslator != null) {
            DataAccessException customDex = customTranslator.translate(task, sql, sqlEx);
            if (customDex != null) {
                return customDex;
            }
        }
    }
    // Check SQLErrorCodes with corresponding error code, if available.
    if (this.sqlErrorCodes != null) {
        String errorCode;
        if (this.sqlErrorCodes.isUseSqlStateForTranslation()) {
            errorCode = sqlEx.getSQLState();
        } else {
            // Try to find SQLException with actual error code, looping through the causes.
            // E.g. applicable to java.sql.DataTruncation as of JDK 1.6.
            SQLException current = sqlEx;
            while (current.getErrorCode() == 0 && current.getCause() instanceof SQLException) {
                current = (SQLException) current.getCause();
            }
            errorCode = Integer.toString(current.getErrorCode());
        }
        if (errorCode != null) {
            // Look for defined custom translations first.
            CustomSQLErrorCodesTranslation[] customTranslations = this.sqlErrorCodes.getCustomTranslations();
            if (customTranslations != null) {
                for (CustomSQLErrorCodesTranslation customTranslation : customTranslations) {
                    if (Arrays.binarySearch(customTranslation.getErrorCodes(), errorCode) >= 0) {
                        if (customTranslation.getExceptionClass() != null) {
                            DataAccessException customException = createCustomException(task, sql, sqlEx, customTranslation.getExceptionClass());
                            if (customException != null) {
                                logTranslation(task, sql, sqlEx, true);
                                return customException;
                            }
                        }
                    }
                }
            }
            // Next, look for grouped error codes.
            if (Arrays.binarySearch(this.sqlErrorCodes.getBadSqlGrammarCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new BadSqlGrammarException(task, sql, sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getInvalidResultSetAccessCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new InvalidResultSetAccessException(task, sql, sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getDuplicateKeyCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new DuplicateKeyException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getDataIntegrityViolationCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new DataIntegrityViolationException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getPermissionDeniedCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new PermissionDeniedDataAccessException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getDataAccessResourceFailureCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new DataAccessResourceFailureException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getTransientDataAccessResourceCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new TransientDataAccessResourceException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getCannotAcquireLockCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new CannotAcquireLockException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getDeadlockLoserCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new DeadlockLoserDataAccessException(buildMessage(task, sql, sqlEx), sqlEx);
            } else if (Arrays.binarySearch(this.sqlErrorCodes.getCannotSerializeTransactionCodes(), errorCode) >= 0) {
                logTranslation(task, sql, sqlEx, false);
                return new CannotSerializeTransactionException(buildMessage(task, sql, sqlEx), sqlEx);
            }
        }
    }
    // We couldn't identify it more precisely - let's hand it over to the SQLState fallback translator.
    if (logger.isDebugEnabled()) {
        String codes;
        if (this.sqlErrorCodes != null && this.sqlErrorCodes.isUseSqlStateForTranslation()) {
            codes = "SQL state '" + sqlEx.getSQLState() + "', error code '" + sqlEx.getErrorCode();
        } else {
            codes = "Error code '" + sqlEx.getErrorCode() + "'";
        }
        logger.debug("Unable to translate SQLException with " + codes + ", will now try the fallback translator");
    }
    return null;
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) TransientDataAccessResourceException(org.springframework.dao.TransientDataAccessResourceException) CannotAcquireLockException(org.springframework.dao.CannotAcquireLockException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) InvalidResultSetAccessException(org.springframework.jdbc.InvalidResultSetAccessException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) CannotSerializeTransactionException(org.springframework.dao.CannotSerializeTransactionException) DeadlockLoserDataAccessException(org.springframework.dao.DeadlockLoserDataAccessException) DataAccessException(org.springframework.dao.DataAccessException) DeadlockLoserDataAccessException(org.springframework.dao.DeadlockLoserDataAccessException) PermissionDeniedDataAccessException(org.springframework.dao.PermissionDeniedDataAccessException) BatchUpdateException(java.sql.BatchUpdateException)

Example 5 with DataAccessResourceFailureException

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

the class DataSourceTransactionManagerTests method doTestTransactionWithTimeout.

private void doTestTransactionWithTimeout(int timeout) throws Exception {
    Assume.group(TestGroup.PERFORMANCE);
    PreparedStatement ps = mock(PreparedStatement.class);
    given(con.getAutoCommit()).willReturn(true);
    given(con.prepareStatement("some SQL statement")).willReturn(ps);
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setTimeout(timeout);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException ex) {
                }
                try {
                    Connection con = DataSourceUtils.getConnection(ds);
                    PreparedStatement ps = con.prepareStatement("some SQL statement");
                    DataSourceUtils.applyTransactionTimeout(ps, ds);
                } catch (SQLException ex) {
                    throw new DataAccessResourceFailureException("", ex);
                }
            }
        });
        if (timeout <= 1) {
            fail("Should have thrown TransactionTimedOutException");
        }
    } catch (TransactionTimedOutException ex) {
        if (timeout <= 1) {
        // expected
        } else {
            throw ex;
        }
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    if (timeout > 1) {
        verify(ps).setQueryTimeout(timeout - 1);
        verify(con).commit();
    } else {
        verify(con).rollback();
    }
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : TransactionTimedOutException(org.springframework.transaction.TransactionTimedOutException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)53 SQLException (java.sql.SQLException)13 IOException (java.io.IOException)10 File (java.io.File)9 Connection (java.sql.Connection)9 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)6 HibernateException (org.hibernate.HibernateException)5 Session (org.hibernate.Session)5 InputStream (java.io.InputStream)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 StorageService (org.dkpro.lab.storage.StorageService)4 StorageKey (org.dkpro.lab.storage.StorageService.StorageKey)4 QueryRuntimeException (org.hisp.dhis.common.QueryRuntimeException)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 Statement (java.sql.Statement)3 InOrder (org.mockito.InOrder)3