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

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

Introduction

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

Prototype

@Override
    public int[] batchUpdate(final String... sql) throws DataAccessException 

Source Link

Usage

From source file:org.pentaho.aggdes.ui.exec.impl.JdbcTemplateSqlExecutor.java

public void execute(final String[] sql, final ExecutorCallback callback) throws DataAccessException {
    Exception exceptionDuringExecute = null;
    DatabaseMeta dbMeta = connectionModel.getDatabaseMeta();
    String url = null;/*from   www  .j  ava  2  s  . co m*/
    try {
        url = dbMeta.getURL();
    } catch (KettleDatabaseException e) {
        throw new DataAccessException("DatabaseMeta problem", e) {
            private static final long serialVersionUID = -3457360074729938909L;
        };
    }
    // create the datasource
    DataSource ds = new SingleConnectionDataSource(dbMeta.getDriverClass(), url, dbMeta.getUsername(),
            dbMeta.getPassword(), false);

    // create the jdbc template
    final JdbcTemplate jt = new JdbcTemplate(ds);

    // create the transaction manager
    DataSourceTransactionManager tsMan = new DataSourceTransactionManager(ds);

    // create the transaction template
    TransactionTemplate txTemplate = new TransactionTemplate(tsMan);

    // set behavior
    txTemplate.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW);
    final String noCommentSql[] = removeCommentsAndSemicolons(connectionModel.getSchema(), sql);
    try {
        // run the code in a transaction
        txTemplate.execute(new TransactionCallbackWithoutResult() {
            public void doInTransactionWithoutResult(TransactionStatus status) {
                jt.batchUpdate(noCommentSql);
            }
        });
    } catch (DataAccessException e) {
        if (logger.isErrorEnabled()) {
            logger.error("data access exception", e);
        }
        exceptionDuringExecute = e;
    }
    callback.executionComplete(exceptionDuringExecute);

}

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

public void testBatchUpdate() throws Exception {
    final String[] sql = { "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 1",
            "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 2" };

    MockControl ctrlStatement = MockControl.createControl(Statement.class);
    Statement mockStatement = (Statement) ctrlStatement.getMock();
    mockStatement.getConnection();//from   ww  w . j a v  a  2  s  .  c o  m
    ctrlStatement.setReturnValue(mockConnection);
    mockStatement.addBatch(sql[0]);
    ctrlStatement.setVoidCallable();
    mockStatement.addBatch(sql[1]);
    ctrlStatement.setVoidCallable();
    mockStatement.executeBatch();
    ctrlStatement.setReturnValue(new int[] { 1, 1 });
    if (debugEnabled) {
        mockStatement.getWarnings();
        ctrlStatement.setReturnValue(null);
    }
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
    DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
    mockDatabaseMetaData.getDatabaseProductName();
    ctrlDatabaseMetaData.setReturnValue("MySQL");
    mockDatabaseMetaData.supportsBatchUpdates();
    ctrlDatabaseMetaData.setReturnValue(true);

    mockConnection.getMetaData();
    ctrlConnection.setReturnValue(mockDatabaseMetaData, 2);
    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    ctrlStatement.replay();
    ctrlDatabaseMetaData.replay();
    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource, false);

    int[] actualRowsAffected = template.batchUpdate(sql);
    assertTrue("executed 2 updates", actualRowsAffected.length == 2);

    ctrlStatement.verify();
    ctrlDatabaseMetaData.verify();
}

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

public void testBatchUpdateWithNoBatchSupport() throws Exception {
    final String[] sql = { "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 1",
            "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 2" };

    MockControl ctrlStatement = MockControl.createControl(Statement.class);
    Statement mockStatement = (Statement) ctrlStatement.getMock();
    mockStatement.getConnection();//from w w  w  .ja  v a2  s.  co  m
    ctrlStatement.setReturnValue(mockConnection);
    mockStatement.execute(sql[0]);
    ctrlStatement.setReturnValue(false);
    mockStatement.getUpdateCount();
    ctrlStatement.setReturnValue(1);
    mockStatement.execute(sql[1]);
    ctrlStatement.setReturnValue(false);
    mockStatement.getUpdateCount();
    ctrlStatement.setReturnValue(1);
    if (debugEnabled) {
        mockStatement.getWarnings();
        ctrlStatement.setReturnValue(null);
    }
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
    DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
    mockDatabaseMetaData.getDatabaseProductName();
    ctrlDatabaseMetaData.setReturnValue("MySQL");
    mockDatabaseMetaData.supportsBatchUpdates();
    ctrlDatabaseMetaData.setReturnValue(false);

    mockConnection.getMetaData();
    ctrlConnection.setReturnValue(mockDatabaseMetaData, 2);
    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    ctrlStatement.replay();
    ctrlDatabaseMetaData.replay();
    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource, false);

    int[] actualRowsAffected = template.batchUpdate(sql);
    assertTrue("executed 2 updates", actualRowsAffected.length == 2);

    ctrlStatement.verify();
    ctrlDatabaseMetaData.verify();
}

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

public void testBatchUpdateWithNoBatchSupportAndSelect() throws Exception {
    final String[] sql = { "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 1",
            "SELECT * FROM NOSUCHTABLE" };

    MockControl ctrlStatement = MockControl.createControl(Statement.class);
    Statement mockStatement = (Statement) ctrlStatement.getMock();
    mockStatement.getConnection();/*ww  w . j  av  a  2  s.  co m*/
    ctrlStatement.setReturnValue(mockConnection);
    mockStatement.execute(sql[0]);
    ctrlStatement.setReturnValue(false);
    mockStatement.getUpdateCount();
    ctrlStatement.setReturnValue(1);
    mockStatement.execute(sql[1]);
    ctrlStatement.setReturnValue(true);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
    DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
    mockDatabaseMetaData.getDatabaseProductName();
    ctrlDatabaseMetaData.setReturnValue("MySQL");
    mockDatabaseMetaData.supportsBatchUpdates();
    ctrlDatabaseMetaData.setReturnValue(false);

    mockConnection.getMetaData();
    ctrlConnection.setReturnValue(mockDatabaseMetaData, 2);
    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

    ctrlStatement.replay();
    ctrlDatabaseMetaData.replay();
    replay();

    JdbcTemplate template = new JdbcTemplate(mockDataSource, false);

    try {
        template.batchUpdate(sql);
        fail("Shouldn't have executed batch statement with a select");
    } catch (DataAccessException ex) {
        // pass
        assertTrue("Check exception type", ex.getClass() == InvalidDataAccessApiUsageException.class);
    }

    ctrlStatement.verify();
    ctrlDatabaseMetaData.verify();
}