List of usage examples for org.springframework.jdbc.core BatchPreparedStatementSetter BatchPreparedStatementSetter
BatchPreparedStatementSetter
From source file:org.openscore.engine.queue.repositories.ExecutionQueueRepositoryImpl.java
@Override public void insertExecutionStates(final List<ExecutionMessage> stateMessages) { String insertExecStateSQL = INSERT_EXEC_STATE; insertExecStateSQL = insertExecStateSQL.replaceAll(":OO_EXECUTION_STATES", getExecStateTableName()); jdbcTemplate.batchUpdate(insertExecStateSQL, new BatchPreparedStatementSetter() { @Override//from w w w. j a va 2s. c o m public void setValues(PreparedStatement ps, int i) throws SQLException { ExecutionMessage msg = stateMessages.get(i); ps.setLong(1, msg.getExecStateId()); ps.setString(2, msg.getMsgId()); ps.setBytes(3, msg.getPayload().getData()); } @Override public int getBatchSize() { return stateMessages.size(); } }); }
From source file:org.openscore.engine.queue.repositories.ExecutionQueueRepositoryImpl.java
@Override public void insertExecutionQueue(final List<ExecutionMessage> messages, final long version) { // insert execution queue table // id, exec_state_id, assigned_worker, status, create_time String insertQueueSQL = INSERT_QUEUE; long t = System.currentTimeMillis(); jdbcTemplate.batchUpdate(insertQueueSQL, new BatchPreparedStatementSetter() { @Override//from w ww . j a va2 s .c o m public void setValues(PreparedStatement ps, int i) throws SQLException { ExecutionMessage msg = messages.get(i); ps.setLong(1, (Long) idGen.next()); ps.setLong(2, msg.getExecStateId()); ps.setString(3, msg.getWorkerId()); ps.setString(4, msg.getWorkerGroup()); ps.setInt(5, msg.getStatus().getNumber()); ps.setInt(6, msg.getMsgSeqId()); ps.setLong(7, version); } @Override public int getBatchSize() { return messages.size(); } }); t = System.currentTimeMillis() - t; if (logger.isDebugEnabled()) logger.debug("Insert to queue: " + messages.size() + "/" + t + " messages/ms"); }
From source file:org.sakaiproject.dash.dao.impl.DashboardDaoImpl.java
public int addCalendarLinks(final List<CalendarLink> calendarLinks) { if (log.isDebugEnabled()) { log.debug("addCalendarLinks( " + calendarLinks.size() + ")"); }//from w ww. j ava2s . com // person_id, item_id, context_id, realm_id int count = 0; try { String sql = getStatement("insert.CalendarLink"); int[] updates = getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { CalendarLink calendarLink = calendarLinks.get(i); ps.setLong(1, calendarLink.getPerson().getId()); ps.setLong(2, calendarLink.getCalendarItem().getId()); ps.setLong(3, calendarLink.getContext().getId()); ps.setBoolean(4, calendarLink.isHidden()); ps.setBoolean(5, calendarLink.isSticky()); } @Override public int getBatchSize() { return calendarLinks.size(); } }); if (updates != null && updates.length > 0) { for (int u : updates) { count += u; } } } catch (DataIntegrityViolationException e) { // this means we're trying to insert a duplicate log.debug("addCalendarLinks() " + e); } catch (DataAccessException ex) { log.warn("addCalendarLinks: Error executing query: " + ex.getClass() + ":" + ex.getMessage()); } return count; }
From source file:org.sakaiproject.dash.dao.impl.DashboardDaoImpl.java
public int addNewsLinks(final List<NewsLink> newsLinks) { if (log.isDebugEnabled()) { log.debug("addNewsLinks( " + newsLinks.size() + ")"); }/* www. j a va 2 s.c o m*/ // person_id, item_id, context_id, realm_id int count = 0; try { String sql = getStatement("insert.NewsLink"); int[] updates = getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { NewsLink newsLink = newsLinks.get(i); ps.setLong(1, newsLink.getPerson().getId()); ps.setLong(2, newsLink.getNewsItem().getId()); ps.setLong(3, newsLink.getContext().getId()); ps.setBoolean(4, newsLink.isHidden()); ps.setBoolean(5, newsLink.isSticky()); } @Override public int getBatchSize() { return newsLinks.size(); } }); if (updates != null && updates.length > 0) { for (int u : updates) { count += u; } } } catch (DataIntegrityViolationException e) { // this means we're trying to insert a duplicate log.debug("addNewsLink() " + e); } catch (DataAccessException ex) { log.warn("addNewsLink: Error executing query: " + ex.getClass() + ":" + ex.getMessage()); } return count; }
From source file:org.sipfoundry.sipxconfig.alarm.AlarmServerManagerImpl.java
@Override public void saveAlarms(final List<Alarm> alarms) { m_jdbcTemplate.execute("delete from alarm_code"); BatchPreparedStatementSetter save = new BatchPreparedStatementSetter() { @Override/*from w w w .j av a2 s.c o m*/ public int getBatchSize() { return alarms.size(); } @Override public void setValues(PreparedStatement ps, int i) throws SQLException { Alarm a = alarms.get(i); ps.setString(1, a.getAlarmDefinition().getId()); ps.setString(2, a.getGroupName()); ps.setInt(3, a.getMinThreshold()); } }; String sql = "insert into alarm_code (alarm_code_id, email_group, min_threshold) values (?, ?, ?)"; m_jdbcTemplate.batchUpdate(sql, save); }
From source file:org.sipfoundry.sipxconfig.firewall.FirewallManagerImpl.java
@Override public void saveRules(List<EditableFirewallRule> rules) { m_jdbc.execute("delete from firewall_rule"); String sql = "insert into firewall_rule (firewall_rule_id, firewall_server_group_id, " + "system_id, address_type, prioritize) values (nextval('firewall_rule_seq'),?,?,?,?)"; final EditableFirewallRule[] changed = getChanged(rules); if (changed.length == 0) { return;//w w w. j a v a 2s . co m } BatchPreparedStatementSetter inserter = new BatchPreparedStatementSetter() { public int getBatchSize() { return changed.length; } public void setValues(PreparedStatement arg, int i) throws SQLException { EditableFirewallRule rule = changed[i]; ServerGroup serverGroup = rule.getServerGroup(); if (serverGroup == null) { arg.setNull(1, Types.INTEGER); arg.setString(2, rule.getSystemId().toString()); } else { arg.setInt(1, serverGroup == null ? 0 : serverGroup.getId()); arg.setNull(2, Types.VARCHAR); } arg.setString(3, rule.getAddressType().getId()); arg.setBoolean(4, rule.isPriority()); } }; m_jdbc.batchUpdate(sql, inserter); }
From source file:org.springframework.batch.core.repository.dao.JdbcStepExecutionDao.java
/** * Batch insert StepExecutions//from w ww . j av a 2s.co m * @see StepExecutionDao#saveStepExecutions(Collection) */ @Override public void saveStepExecutions(final Collection<StepExecution> stepExecutions) { Assert.notNull(stepExecutions, "Attempt to save a null collection of step executions"); if (!stepExecutions.isEmpty()) { final Iterator<StepExecution> iterator = stepExecutions.iterator(); getJdbcTemplate().batchUpdate(getQuery(SAVE_STEP_EXECUTION), new BatchPreparedStatementSetter() { @Override public int getBatchSize() { return stepExecutions.size(); } @Override public void setValues(PreparedStatement ps, int i) throws SQLException { StepExecution stepExecution = iterator.next(); List<Object[]> parameters = buildStepExecutionParameters(stepExecution); Object[] parameterValues = parameters.get(0); Integer[] parameterTypes = (Integer[]) parameters.get(1); for (int indx = 0; indx < parameterValues.length; indx++) { switch (parameterTypes[indx]) { case Types.INTEGER: ps.setInt(indx + 1, (Integer) parameterValues[indx]); break; case Types.VARCHAR: ps.setString(indx + 1, (String) parameterValues[indx]); break; case Types.TIMESTAMP: if (parameterValues[indx] != null) { ps.setTimestamp(indx + 1, new Timestamp(((java.util.Date) parameterValues[indx]).getTime())); } else { ps.setNull(indx + 1, Types.TIMESTAMP); } break; case Types.BIGINT: ps.setLong(indx + 1, (Long) parameterValues[indx]); break; default: throw new IllegalArgumentException( "unsupported SQL parameter type for step execution field index " + i); } } } }); } }
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
public void testBatchUpdateWithPreparedStatement() throws Exception { final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; final int[] ids = new int[] { 100, 200 }; final int[] rowsAffected = new int[] { 1, 2 }; MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class); PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock(); mockPreparedStatement.getConnection(); ctrlPreparedStatement.setReturnValue(mockConnection); mockPreparedStatement.setInt(1, ids[0]); ctrlPreparedStatement.setVoidCallable(); mockPreparedStatement.addBatch();/*w ww . ja v a2 s .c o m*/ ctrlPreparedStatement.setVoidCallable(); mockPreparedStatement.setInt(1, ids[1]); ctrlPreparedStatement.setVoidCallable(); mockPreparedStatement.addBatch(); ctrlPreparedStatement.setVoidCallable(); mockPreparedStatement.executeBatch(); ctrlPreparedStatement.setReturnValue(rowsAffected); if (debugEnabled) { mockPreparedStatement.getWarnings(); ctrlPreparedStatement.setReturnValue(null); } mockPreparedStatement.close(); ctrlPreparedStatement.setVoidCallable(); MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class); DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock(); mockDatabaseMetaData.getDatabaseProductName(); ctrlDatabaseMetaData.setReturnValue("MySQL"); mockDatabaseMetaData.supportsBatchUpdates(); ctrlDatabaseMetaData.setReturnValue(true); mockConnection.prepareStatement(sql); ctrlConnection.setReturnValue(mockPreparedStatement); mockConnection.getMetaData(); ctrlConnection.setReturnValue(mockDatabaseMetaData, 2); ctrlPreparedStatement.replay(); ctrlDatabaseMetaData.replay(); replay(); BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() { public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setInt(1, ids[i]); } public int getBatchSize() { return ids.length; } }; JdbcTemplate template = new JdbcTemplate(mockDataSource, false); int[] actualRowsAffected = template.batchUpdate(sql, setter); assertTrue("executed 2 updates", actualRowsAffected.length == 2); assertEquals(rowsAffected[0], actualRowsAffected[0]); assertEquals(rowsAffected[1], actualRowsAffected[1]); ctrlPreparedStatement.verify(); ctrlDatabaseMetaData.verify(); }
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
public void testBatchUpdateWithPreparedStatementAndNoBatchSupport() throws Exception { final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; final int[] ids = new int[] { 100, 200 }; final int[] rowsAffected = new int[] { 1, 2 }; MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class); PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock(); mockPreparedStatement.getConnection(); ctrlPreparedStatement.setReturnValue(mockConnection); mockPreparedStatement.setInt(1, ids[0]); ctrlPreparedStatement.setVoidCallable(); mockPreparedStatement.executeUpdate(); ctrlPreparedStatement.setReturnValue(rowsAffected[0]); mockPreparedStatement.setInt(1, ids[1]); ctrlPreparedStatement.setVoidCallable(); mockPreparedStatement.executeUpdate(); ctrlPreparedStatement.setReturnValue(rowsAffected[1]); if (debugEnabled) { mockPreparedStatement.getWarnings(); ctrlPreparedStatement.setReturnValue(null); }/* w w w.ja va 2s . c o m*/ mockPreparedStatement.close(); ctrlPreparedStatement.setVoidCallable(); mockConnection.prepareStatement(sql); ctrlConnection.setReturnValue(mockPreparedStatement); ctrlPreparedStatement.replay(); replay(); BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() { public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setInt(1, ids[i]); } public int getBatchSize() { return ids.length; } }; JdbcTemplate template = new JdbcTemplate(mockDataSource); int[] actualRowsAffected = template.batchUpdate(sql, setter); assertTrue("executed 2 updates", actualRowsAffected.length == 2); assertEquals(rowsAffected[0], actualRowsAffected[0]); assertEquals(rowsAffected[1], actualRowsAffected[1]); ctrlPreparedStatement.verify(); }
From source file:org.springframework.jdbc.core.JdbcTemplateTests.java
public void testBatchUpdateFails() throws Exception { final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; final int[] ids = new int[] { 100, 200 }; SQLException sex = new SQLException(); MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class); PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock(); mockPreparedStatement.getConnection(); ctrlPreparedStatement.setReturnValue(mockConnection); mockPreparedStatement.setInt(1, ids[0]); ctrlPreparedStatement.setVoidCallable(); mockPreparedStatement.addBatch();/*from w w w . j a va 2 s. c o m*/ ctrlPreparedStatement.setVoidCallable(); mockPreparedStatement.setInt(1, ids[1]); ctrlPreparedStatement.setVoidCallable(); mockPreparedStatement.addBatch(); ctrlPreparedStatement.setVoidCallable(); mockPreparedStatement.executeBatch(); ctrlPreparedStatement.setThrowable(sex); mockPreparedStatement.close(); ctrlPreparedStatement.setVoidCallable(); MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class); DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock(); mockDatabaseMetaData.getDatabaseProductName(); ctrlDatabaseMetaData.setReturnValue("MySQL"); mockDatabaseMetaData.supportsBatchUpdates(); ctrlDatabaseMetaData.setReturnValue(true); ctrlConnection.reset(); mockConnection.prepareStatement(sql); ctrlConnection.setReturnValue(mockPreparedStatement); mockConnection.getMetaData(); ctrlConnection.setReturnValue(mockDatabaseMetaData, 2); mockConnection.close(); ctrlConnection.setVoidCallable(2); ctrlPreparedStatement.replay(); ctrlDatabaseMetaData.replay(); replay(); BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() { public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setInt(1, ids[i]); } public int getBatchSize() { return ids.length; } }; try { JdbcTemplate template = new JdbcTemplate(mockDataSource); template.batchUpdate(sql, setter); fail("Should have failed because of SQLException in bulk update"); } catch (DataAccessException ex) { assertTrue("Root cause is SQLException", ex.getCause() == sex); } ctrlPreparedStatement.verify(); ctrlDatabaseMetaData.verify(); }