Skip to content

Commit 781faf1

Browse files
hpoettkerfmbenhassine
authored andcommitted
Fix multi-threaded empty read for JdbcPagingItemReader
On empty input, the JdbcPagingItemReader cannot derive a start value for the sort key to be used in further queries. For multi-threaded steps, it is thus necessary to prevent the reader from trying to read further pages if the first page is empty. Issue #3898
1 parent 86d03d0 commit 781faf1

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcPagingItemReader.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.sql.ResultSet;
2020
import java.sql.SQLException;
2121
import java.util.ArrayList;
22-
import java.util.Collection;
22+
import java.util.Collections;
2323
import java.util.LinkedHashMap;
2424
import java.util.List;
2525
import java.util.Map;
@@ -179,7 +179,6 @@ public void afterPropertiesSet() throws Exception {
179179
}
180180

181181
@Override
182-
@SuppressWarnings("unchecked")
183182
protected void doReadPage() {
184183
if (results == null) {
185184
results = new CopyOnWriteArrayList<>();
@@ -190,7 +189,7 @@ protected void doReadPage() {
190189

191190
PagingRowMapper rowCallback = new PagingRowMapper();
192191

193-
List<?> query;
192+
List<T> query;
194193

195194
if (getPage() == 0) {
196195
if (logger.isDebugEnabled()) {
@@ -211,7 +210,7 @@ protected void doReadPage() {
211210
}
212211

213212
}
214-
else {
213+
else if (startAfterValues != null) {
215214
previousStartAfterValues = startAfterValues;
216215
if (logger.isDebugEnabled()) {
217216
logger.debug("SQL used for reading remaining pages: [" + remainingPagesSql + "]");
@@ -225,9 +224,11 @@ protected void doReadPage() {
225224
getParameterList(parameterValues, startAfterValues).toArray(), rowCallback);
226225
}
227226
}
227+
else {
228+
query = Collections.emptyList();
229+
}
228230

229-
Collection<T> result = (Collection<T>) query;
230-
results.addAll(result);
231+
results.addAll(query);
231232
}
232233

233234
@Override

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcPagingItemReaderAsyncTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2014 the original author or authors.
2+
* Copyright 2009-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,7 +80,8 @@ public class JdbcPagingItemReaderAsyncTests {
8080
@Before
8181
public void init() {
8282
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
83-
maxId = jdbcTemplate.queryForObject("SELECT MAX(ID) from T_FOOS", Integer.class);
83+
Integer maxIdResult = jdbcTemplate.queryForObject("SELECT MAX(ID) from T_FOOS", Integer.class);
84+
maxId = maxIdResult == null ? 0 : maxIdResult;
8485
for (int i = maxId + 1; i <= ITEM_COUNT; i++) {
8586
jdbcTemplate.update("INSERT into T_FOOS (ID,NAME,VALUE) values (?, ?, ?)", i, "foo" + i, i);
8687
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.item.database;
17+
18+
import static org.junit.Assert.assertNull;
19+
20+
import java.util.Collections;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
import org.springframework.batch.item.ItemReader;
28+
import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.jdbc.core.SingleColumnRowMapper;
31+
import org.springframework.test.context.ContextConfiguration;
32+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
33+
34+
@RunWith(SpringJUnit4ClassRunner.class)
35+
@ContextConfiguration(locations = "JdbcPagingItemReaderCommonTests-context.xml")
36+
public class JdbcPagingItemReaderEmptyResultSetTests {
37+
38+
private static final int PAGE_SIZE = 2;
39+
private static final int EMPTY_READS = PAGE_SIZE + 1;
40+
41+
@Autowired
42+
private DataSource dataSource;
43+
44+
@Test
45+
public void testMultiplePageReadsOnEmptyResultSet() throws Exception {
46+
final ItemReader<Long> reader = getItemReader();
47+
for (int i = 0; i < EMPTY_READS; i++) {
48+
assertNull(reader.read());
49+
}
50+
}
51+
52+
private ItemReader<Long> getItemReader() throws Exception {
53+
JdbcPagingItemReader<Long> reader = new JdbcPagingItemReader<>();
54+
reader.setDataSource(dataSource);
55+
HsqlPagingQueryProvider queryProvider = new HsqlPagingQueryProvider();
56+
queryProvider.setSelectClause("select ID");
57+
queryProvider.setFromClause("from T_FOOS");
58+
queryProvider.setWhereClause("1 = 0");
59+
queryProvider.setSortKeys(Collections.singletonMap("ID", Order.ASCENDING));
60+
reader.setQueryProvider(queryProvider);
61+
reader.setRowMapper(new SingleColumnRowMapper<>());
62+
reader.setPageSize(PAGE_SIZE);
63+
reader.afterPropertiesSet();
64+
reader.setSaveState(false);
65+
66+
return reader;
67+
}
68+
}

0 commit comments

Comments
 (0)