Example usage for org.springframework.jdbc.core JdbcTemplate setLazyInit

List of usage examples for org.springframework.jdbc.core JdbcTemplate setLazyInit

Introduction

In this page you can find the example usage for org.springframework.jdbc.core JdbcTemplate setLazyInit.

Prototype

public void setLazyInit(boolean lazyInit) 

Source Link

Document

Set whether to lazily initialize the SQLExceptionTranslator for this accessor, on first encounter of a SQLException.

Usage

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

/**
 * If beanProperty is true, initialize via exception translator bean property;
 * if false, use afterPropertiesSet().//from w ww . ja v a  2 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();
}