Example usage for org.springframework.jdbc CannotGetJdbcConnectionException getCause

List of usage examples for org.springframework.jdbc CannotGetJdbcConnectionException getCause

Introduction

In this page you can find the example usage for org.springframework.jdbc CannotGetJdbcConnectionException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:org.finra.dm.service.impl.JdbcServiceImpl.java

/**
 * Executes a single statement using the given JDBC template. The given statement will be updated with the result and status.
 * /*  w  w w .  j  a va  2  s .  c  o  m*/
 * @param jdbcTemplate JDBC template
 * @param jdbcStatement JDBC statement to execute
 * @param variables
 * @param jdbcStatementIndex
 */
private void executeStatement(JdbcTemplate jdbcTemplate, JdbcStatement jdbcStatement,
        Map<String, Object> variables, int jdbcStatementIndex) {
    // This is the exception to be set as the error message in the response
    Throwable exception = null;
    try {
        String sql = evaluate(jdbcStatement.getSql(), variables, "jdbc statement sql");
        validateSqlStatement(sql, jdbcStatementIndex);

        // Process UPDATE type statements
        if (JdbcStatementType.UPDATE.equals(jdbcStatement.getType())) {
            int result = jdbcDao.update(jdbcTemplate, sql);

            jdbcStatement.setStatus(JdbcStatementStatus.SUCCESS);
            jdbcStatement.setResult(String.valueOf(result));
        }
        // Process QUERY type statements
        else if (JdbcStatementType.QUERY.equals(jdbcStatement.getType())) {
            Integer maxResults = configurationHelper.getProperty(ConfigurationValue.JDBC_RESULT_MAX_ROWS,
                    Integer.class);

            JdbcStatementResultSet jdbcStatementResultSet = jdbcDao.query(jdbcTemplate, sql, maxResults);

            jdbcStatement.setStatus(JdbcStatementStatus.SUCCESS);
            jdbcStatement.setResultSet(jdbcStatementResultSet);
        }
        // Any other statement types are unrecognized. This case should not be possible unless developer error.
        else {
            throw new IllegalStateException(
                    "Unsupported JDBC statement type '" + jdbcStatement.getType() + "'");
        }
    } catch (CannotGetJdbcConnectionException cannotGetJdbcConnectionException) {
        /*
         * When the statement fails to execute due to connection errors. This usually indicates that the connection information which was specified is
         * wrong, or there is a network issue. Either way, it would indicate user error.
         * We get the wrapped exception and throw again as an IllegalArgumentException.
         */
        Throwable causeThrowable = cannotGetJdbcConnectionException.getCause();
        throw new IllegalArgumentException(String.valueOf(causeThrowable).trim(),
                cannotGetJdbcConnectionException);
    } catch (DataAccessException dataAccessException) {
        // DataAccessException's cause is a SQLException which is thrown by driver
        // We will use the SQLException message result
        exception = dataAccessException.getCause();
    }

    // If there was an error
    if (exception != null) {
        // Set status to error and result as message
        jdbcStatement.setStatus(JdbcStatementStatus.ERROR);
        jdbcStatement.setErrorMessage(maskSensitiveInformation(exception, variables));
    }
}

From source file:org.springframework.jdbc.core.JdbcTemplateTests.java

public void testCouldntGetConnectionOrExceptionTranslator() throws SQLException {
    SQLException sex = new SQLException("foo", "07xxx");

    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    mockDataSource.getConnection();//w  ww .  ja  v a2s .  c  o m
    // Expect two calls (one call after caching data product name): make get metadata fail also
    ctrlDataSource.setThrowable(sex, 2);
    replay();

    try {
        JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
        RowCountCallbackHandler rcch = new RowCountCallbackHandler();
        template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
        fail("Shouldn't have executed query without a connection");
    } catch (CannotGetJdbcConnectionException ex) {
        // pass
        assertTrue("Check root cause", ex.getCause() == sex);
    }

    ctrlDataSource.verify();
}

From source file:org.springframework.jdbc.core.JdbcTemplateTests.java

public void testCouldntGetConnectionForOperationOrExceptionTranslator() throws SQLException {
    SQLException sex = new SQLException("foo", "07xxx");

    // Change behavior in setUp() because we only expect one call to getConnection():
    // none is necessary to get metadata for exception translator
    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    mockDataSource.getConnection();//from   w w  w.  j  a  va  2s.  co m
    // Expect two calls (one call after caching data product name): make get Metadata fail also
    ctrlDataSource.setThrowable(sex, 2);
    replay();

    try {
        JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
        RowCountCallbackHandler rcch = new RowCountCallbackHandler();
        template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
        fail("Shouldn't have executed query without a connection");
    } catch (CannotGetJdbcConnectionException ex) {
        // pass
        assertTrue("Check root cause", ex.getCause() == sex);
    }

    ctrlDataSource.verify();
}

From source file:org.springframework.jdbc.core.JdbcTemplateTests.java

public void testCouldntGetConnectionForOperationWithLazyExceptionTranslator() throws SQLException {
    SQLException sex = new SQLException("foo", "07xxx");

    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    mockDataSource.getConnection();/*from www .j  av  a 2s .com*/
    ctrlDataSource.setThrowable(sex, 1);
    replay();

    try {
        JdbcTemplate template2 = new JdbcTemplate();
        template2.setDataSource(mockDataSource);
        template2.afterPropertiesSet();
        RowCountCallbackHandler rcch = new RowCountCallbackHandler();
        template2.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
        fail("Shouldn't have executed query without a connection");
    } catch (CannotGetJdbcConnectionException ex) {
        // pass
        assertTrue("Check root cause", ex.getCause() == sex);
    }

    ctrlDataSource.verify();
}

From source file:org.springframework.jdbc.core.JdbcTemplateTests.java

/**
 * Verify that afterPropertiesSet invokes exception translator.
 *///w ww.  j  a va2 s.  c  o  m
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitialized() throws SQLException {
    SQLException sex = new SQLException("foo", "07xxx");

    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    //mockConnection.getMetaData();
    //ctrlConnection.setReturnValue(null, 1);
    //mockConnection.close();
    //ctrlConnection.setVoidCallable(1);
    ctrlConnection.replay();

    // Change behaviour in setUp() because we only expect one call to getConnection():
    // none is necessary to get metadata for exception translator
    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    // Upfront call for metadata - no longer the case
    //mockDataSource.getConnection();
    //ctrlDataSource.setReturnValue(mockConnection, 1);
    // One call for operation
    mockDataSource.getConnection();
    ctrlDataSource.setThrowable(sex, 2);
    ctrlDataSource.replay();

    try {
        JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
        RowCountCallbackHandler rcch = new RowCountCallbackHandler();
        template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
        fail("Shouldn't have executed query without a connection");
    } catch (CannotGetJdbcConnectionException ex) {
        // pass
        assertTrue("Check root cause", ex.getCause() == sex);
    }

    ctrlDataSource.verify();
    ctrlConnection.verify();
}

From source file:org.springframework.jdbc.core.JdbcTemplateTests.java

/**
 * If beanProperty is true, initialize via exception translator bean property;
 * if false, use afterPropertiesSet()./*from  ww  w. j  a v a2 s  .  c o  m*/
 */
private void doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty)
        throws SQLException {

    SQLException sex = new SQLException("foo", "07xxx");

    ctrlConnection = MockControl.createControl(Connection.class);
    mockConnection = (Connection) ctrlConnection.getMock();
    //mockConnection.getMetaData();
    //ctrlConnection.setReturnValue(null, 1);
    //mockConnection.close();
    //ctrlConnection.setVoidCallable(1);
    ctrlConnection.replay();

    // Change behaviour in setUp() because we only expect one call to getConnection():
    // none is necessary to get metadata for exception translator
    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    // Upfront call for metadata - no longer the case
    //mockDataSource.getConnection();
    //ctrlDataSource.setReturnValue(mockConnection, 1);
    // One call for operation
    mockDataSource.getConnection();
    ctrlDataSource.setThrowable(sex, 2);
    ctrlDataSource.replay();

    try {
        JdbcTemplate template2 = new JdbcTemplate();
        template2.setDataSource(mockDataSource);
        template2.setLazyInit(false);
        if (beanProperty) {
            // This will get a connection.
            template2.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(mockDataSource));
        } else {
            // This will cause creation of default SQL translator.
            // Note that only call should be effective.
            template2.afterPropertiesSet();
            template2.afterPropertiesSet();
        }
        RowCountCallbackHandler rcch = new RowCountCallbackHandler();
        template2.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
        fail("Shouldn't have executed query without a connection");
    } catch (CannotGetJdbcConnectionException ex) {
        // pass
        assertTrue("Check root cause", ex.getCause() == sex);
    }

    ctrlDataSource.verify();
    ctrlConnection.verify();
}