@Override public Map<String, ExecutionContext> partition(int gridSize) { int min = jdbcTemplate.queryForObject("SELECT MIN(" + column + ") from " + table, Integer.class); int max = jdbcTemplate.queryForObject("SELECT MAX(" + column + ") from " + table, Integer.class); int targetSize = (max - min) / gridSize; Map<String, ExecutionContext> result = new HashMap<String, ExecutionContext>(); int number = 0; int start = min; int end = start + targetSize - 1; while (start <= max) { ExecutionContext value = new ExecutionContext(); result.put("partition" + number, value); if (end >= max) { end = max; } value.putInt("minValue", start); value.putInt("maxValue", end); start += targetSize; end += targetSize; number++; } return result; }
@Override public ProcessIndicatorItemWrapper<T> read() { if (!initialized) { throw new ReaderNotOpenException("Reader must be open before it can be used."); } Long id = null; synchronized (lock) { if (keys.hasNext()) { id = keys.next(); } } logger.debug("Retrieved key from list: " + id); if (id == null) { return null; } @SuppressWarnings("unchecked") T result = (T) jdbcTemplate.queryForObject( "SELECT VALUE FROM BATCH_STAGING WHERE ID=?", new RowMapper<Object>() { @Override public Object mapRow(ResultSet rs, int rowNum) throws SQLException { byte[] blob = rs.getBytes(1); return SerializationUtils.deserialize(blob); } }, id); return new ProcessIndicatorItemWrapper<T>(id, result); }
private void checkOutputTable(int before) { @SuppressWarnings("serial") final List<Trade> trades = new ArrayList<Trade>() { { add(new Trade("UK21341EAH41", 211, new BigDecimal("31.11"), "customer1")); add(new Trade("UK21341EAH42", 212, new BigDecimal("32.11"), "customer2")); add(new Trade("UK21341EAH43", 213, new BigDecimal("33.11"), "customer3")); add(new Trade("UK21341EAH44", 214, new BigDecimal("34.11"), "customer4")); add(new Trade("UK21341EAH45", 215, new BigDecimal("35.11"), "customer5")); } }; int after = jdbcTemplate.queryForObject("SELECT COUNT(*) from TRADE", Integer.class); assertEquals(before + 5, after); jdbcTemplate.query( GET_TRADES, new RowCallbackHandler() { private int activeRow = 0; @Override public void processRow(ResultSet rs) throws SQLException { Trade trade = trades.get(activeRow++); assertEquals(trade.getIsin(), rs.getString(1)); assertEquals(trade.getQuantity(), rs.getLong(2)); assertEquals(trade.getPrice(), rs.getBigDecimal(3)); assertEquals(trade.getCustomer(), rs.getString(4)); } }); }
@Test public void testJobLaunch() throws Exception { jdbcTemplate.update("DELETE from TRADE"); int before = jdbcTemplate.queryForObject("SELECT COUNT(*) from TRADE", Integer.class); jobLauncherTestUtils.launchJob(); checkOutputFile("target/test-outputs/CustomerReport1.txt"); checkOutputFile("target/test-outputs/CustomerReport2.txt"); checkOutputTable(before); }
@Test public void testDataSourceInitialized() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:true"); this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); DataSource dataSource = this.context.getBean(DataSource.class); assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue(); assertThat(dataSource).isNotNull(); JdbcOperations template = new JdbcTemplate(dataSource); assertThat(template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)).isEqualTo(1); }
@Test public void testDataSourceInitializedWithMultipleScripts() throws Exception { EnvironmentTestUtils.addEnvironment( this.context, "spring.datasource.initialize:true", "spring.datasource.schema:" + ClassUtils.addResourcePathToPackagePath(getClass(), "schema.sql") + "," + ClassUtils.addResourcePathToPackagePath(getClass(), "another.sql"), "spring.datasource.data:" + ClassUtils.addResourcePathToPackagePath(getClass(), "data.sql")); this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); DataSource dataSource = this.context.getBean(DataSource.class); assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue(); assertThat(dataSource).isNotNull(); JdbcOperations template = new JdbcTemplate(dataSource); assertThat(template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)).isEqualTo(1); assertThat(template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class)).isEqualTo(0); }
@Test public void testDataSourceInitialized() throws Exception { this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); DataSource dataSource = this.context.getBean(DataSource.class); assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); assertNotNull(dataSource); JdbcOperations template = new JdbcTemplate(dataSource); assertEquals( new Integer(0), template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)); }
@Test public void testDataSourceInitializedWithMultipleScripts() throws Exception { this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); Map<String, Object> map = new HashMap<String, Object>(); map.put( DataSourceAutoConfiguration.CONFIGURATION_PREFIX + ".schema", ClassUtils.addResourcePathToPackagePath(getClass(), "schema.sql") + "," + ClassUtils.addResourcePathToPackagePath(getClass(), "another.sql")); this.context.getEnvironment().getPropertySources().addFirst(new MapPropertySource("test", map)); this.context.refresh(); DataSource dataSource = this.context.getBean(DataSource.class); assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource); assertNotNull(dataSource); JdbcOperations template = new JdbcTemplate(dataSource); assertEquals( new Integer(0), template.queryForObject("SELECT COUNT(*) from FOO", Integer.class)); assertEquals( new Integer(0), template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class)); }
@Test public void testDataSourceInitializedWithExplicitSqlScriptEncoding() throws Exception { this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); EnvironmentTestUtils.addEnvironment( this.context, "spring.datasource.initialize:true", "spring.datasource.sqlScriptEncoding:UTF-8", "spring.datasource.schema:" + ClassUtils.addResourcePathToPackagePath(getClass(), "encoding-schema.sql"), "spring.datasource.data:" + ClassUtils.addResourcePathToPackagePath(getClass(), "encoding-data.sql")); this.context.refresh(); DataSource dataSource = this.context.getBean(DataSource.class); assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue(); assertThat(dataSource).isNotNull(); JdbcOperations template = new JdbcTemplate(dataSource); assertThat(template.queryForObject("SELECT COUNT(*) from BAR", Integer.class)).isEqualTo(2); assertThat(template.queryForObject("SELECT name from BAR WHERE id=1", String.class)) .isEqualTo("bar"); assertThat(template.queryForObject("SELECT name from BAR WHERE id=2", String.class)) .isEqualTo("ばー"); }
public MessageGroup addMessageToGroup(Object groupId, Message<?> message) { final String groupKey = getKey(groupId); final String messageId = getKey(message.getHeaders().getId()); boolean groupNotExist = jdbcTemplate.queryForInt(this.getQuery(Query.GROUP_EXISTS), groupKey, region) < 1; final Timestamp updatedDate = new Timestamp(System.currentTimeMillis()); final Timestamp createdDate = groupNotExist ? updatedDate : jdbcTemplate.queryForObject( getQuery(Query.GET_GROUP_CREATED_DATE), new Object[] {groupKey, region}, Timestamp.class); if (groupNotExist) { try { this.doCreateMessageGroup(groupKey, createdDate); } catch (DuplicateKeyException e) { logger.warn("Lost race to create group; attempting update instead", e); this.doUpdateMessageGroup(groupKey, updatedDate); } } else { this.doUpdateMessageGroup(groupKey, updatedDate); } this.addMessage(message); jdbcTemplate.update( getQuery(Query.CREATE_GROUP_TO_MESSAGE), new PreparedStatementSetter() { public void setValues(PreparedStatement ps) throws SQLException { if (logger.isDebugEnabled()) { logger.debug( "Inserting message with id key=" + messageId + " and created date=" + createdDate); } ps.setString(1, groupKey); ps.setString(2, messageId); ps.setString(3, region); } }); return getMessageGroup(groupId); }
@Test public void testInitializationDisabled() throws Exception { this.context.register( DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); DataSource dataSource = this.context.getBean(DataSource.class); this.context.publishEvent(new DataSourceInitializedEvent(dataSource)); assertThat(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource).isTrue(); assertThat(dataSource).isNotNull(); JdbcOperations template = new JdbcTemplate(dataSource); try { template.queryForObject("SELECT COUNT(*) from BAR", Integer.class); fail("Query should have failed as BAR table does not exist"); } catch (BadSqlGrammarException ex) { SQLException sqlException = ex.getSQLException(); int expectedCode = -5501; // user lacks privilege or object not found assertThat(sqlException.getErrorCode()).isEqualTo(expectedCode); } }
@Override @Transactional(readOnly = true) public Event getEvent(int eventId) { return jdbcOperations.queryForObject(EVENT_QUERY + " and e.id = ?", EVENT_ROW_MAPPER, eventId); }