Skip to content

Commit 169fe17

Browse files
committed
Reset page and item count on RepositoryItemReader#close()
When closing the RepositoryItemReader, this commit now resets the current item index and page index to be 0 so that it can be reused imediately (instead of requiring the reader be step scoped for the reset to occur). BATCH-2365
1 parent adc2f79 commit 169fe17

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemReader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ protected void doOpen() throws Exception {
216216

217217
@Override
218218
protected void doClose() throws Exception {
219+
synchronized (lock) {
220+
current = 0;
221+
page = 0;
222+
results = null;
223+
}
219224
}
220225

221226
private Sort convertToSort(Map<String, Sort.Direction> sorts) {

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/RepositoryItemReaderTests.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public void testDoReadFirstReadExhausted() throws Exception {
185185
public void testJumpToItem() throws Exception {
186186
reader.setPageSize(100);
187187
ArgumentCaptor<PageRequest> pageRequestContainer = ArgumentCaptor.forClass(PageRequest.class);
188-
when(repository.findAll(pageRequestContainer.capture())).thenReturn(new PageImpl<Object>(new ArrayList<Object>(){{
188+
when(repository.findAll(pageRequestContainer.capture())).thenReturn(new PageImpl<Object>(new ArrayList<Object>() {{
189189
add(new Object());
190190
}}));
191191

@@ -241,7 +241,7 @@ public void testSettingCurrentItemCountExplicitly() throws Exception {
241241
reader.setPageSize(2);
242242

243243
PageRequest request = new PageRequest(1, 2, new Sort(Direction.ASC, "id"));
244-
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>(){{
244+
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>() {{
245245
add("3");
246246
add("4");
247247
}}));
@@ -274,7 +274,7 @@ public void testSettingCurrentItemCountRestart() throws Exception {
274274
}}));
275275

276276
request = new PageRequest(2, 2, new Sort(Direction.ASC, "id"));
277-
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>(){{
277+
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>() {{
278278
add("5");
279279
add("6");
280280
}}));
@@ -294,6 +294,36 @@ public void testSettingCurrentItemCountRestart() throws Exception {
294294
assertEquals("6", reader.read());
295295
}
296296

297+
@Test
298+
public void testResetOfPage() throws Exception {
299+
reader.setPageSize(2);
300+
301+
PageRequest request = new PageRequest(0, 2, new Sort(Direction.ASC, "id"));
302+
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>(){{
303+
add("1");
304+
add("2");
305+
}}));
306+
307+
request = new PageRequest(1, 2, new Sort(Direction.ASC, "id"));
308+
when(repository.findAll(request)).thenReturn(new PageImpl<Object>(new ArrayList<Object>() {{
309+
add("3");
310+
add("4");
311+
}}));
312+
313+
ExecutionContext executionContext = new ExecutionContext();
314+
reader.open(executionContext);
315+
316+
Object result = reader.read();
317+
reader.close();
318+
319+
assertEquals("1", result);
320+
321+
reader.open(executionContext);
322+
assertEquals("1", reader.read());
323+
assertEquals("2", reader.read());
324+
assertEquals("3", reader.read());
325+
}
326+
297327
public static interface TestRepository extends PagingAndSortingRepository<Map, Long> {
298328
Page<String> findFirstNames(Pageable pageable);
299329
}

0 commit comments

Comments
 (0)