Skip to content

Commit 78ddf44

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 e422f59 commit 78ddf44

File tree

6 files changed

+94
-36
lines changed

6 files changed

+94
-36
lines changed

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

Lines changed: 7 additions & 6 deletions
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());
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

Lines changed: 3 additions & 2 deletions
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+
}

spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/JdbcPagingItemReaderCommonTests-context.xml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xmlns:util="http://www.springframework.org/schema/util"
3+
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
44
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
5-
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
5+
http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
66

7-
<bean class="test.jdbc.datasource.DataSourceInitializer">
8-
<property name="dataSource" ref="dataSource"/>
9-
<property name="initScripts" value="org/springframework/batch/item/database/init-foo-schema-hsqldb.sql" />
10-
</bean>
11-
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
12-
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
13-
<property name="url" value="jdbc:hsqldb:mem:testdb" />
14-
</bean>
7+
<jdbc:embedded-database id="dataSource" generate-name="true">
8+
<jdbc:script location="org/springframework/batch/item/database/init-foo-schema-hsqldb.sql"/>
9+
</jdbc:embedded-database>
1510

1611
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
1712
<property name="dataSource" ref="dataSource" />

spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderCommonTests-context.xml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xmlns:util="http://www.springframework.org/schema/util"
3+
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
44
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
5-
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
5+
http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
66

7-
<bean class="test.jdbc.datasource.DataSourceInitializer">
8-
<property name="dataSource" ref="dataSource"/>
9-
<property name="initScripts" value="org/springframework/batch/item/database/init-foo-schema-hsqldb.sql" />
10-
</bean>
11-
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
12-
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
13-
<property name="url" value="jdbc:hsqldb:mem:testdb" />
14-
</bean>
7+
<jdbc:embedded-database id="dataSource" generate-name="true">
8+
<jdbc:script location="org/springframework/batch/item/database/init-foo-schema-hsqldb.sql"/>
9+
</jdbc:embedded-database>
1510

1611
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
1712
<property name="entityManagerFactory" ref="entityManagerFactory" />

spring-batch-infrastructure/src/test/resources/org/springframework/batch/item/database/data-source-context.xml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
45
xmlns:tx="http://www.springframework.org/schema/tx"
56
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd
68
http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
79

10+
<jdbc:embedded-database id="dataSource" generate-name="true">
11+
<jdbc:script location="org/springframework/batch/item/database/init-foo-schema-hsqldb.sql"/>
12+
</jdbc:embedded-database>
13+
814
<tx:annotation-driven/>
915

10-
<bean class="test.jdbc.datasource.DataSourceInitializer">
11-
<property name="dataSource" ref="dataSource"/>
12-
<property name="initScripts" value="org/springframework/batch/item/database/init-foo-schema-hsqldb.sql" />
13-
</bean>
14-
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
15-
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
16-
<property name="url" value="jdbc:hsqldb:mem:testdb" />
17-
</bean>
1816
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
1917
<property name="dataSource" ref="dataSource" />
2018
</bean>

0 commit comments

Comments
 (0)