Skip to content

Commit 2bd3c15

Browse files
hpoettkerfmbenhassine
authored andcommitted
Add state reset on close of AbstractPaginatedDataItemReader
Resolves #1086
1 parent 7678949 commit 2bd3c15

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2023 the original author or authors.
2+
* Copyright 2013-2024 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.
@@ -103,6 +103,14 @@ protected void doOpen() throws Exception {
103103

104104
@Override
105105
protected void doClose() throws Exception {
106+
this.lock.lock();
107+
try {
108+
this.page = 0;
109+
this.results = null;
110+
}
111+
finally {
112+
this.lock.unlock();
113+
}
106114
}
107115

108116
@Override

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2023 the original author or authors.
2+
* Copyright 2013-2024 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.
@@ -18,6 +18,7 @@
1818
import java.util.ArrayList;
1919
import java.util.Collections;
2020
import java.util.HashMap;
21+
import java.util.List;
2122
import java.util.Map;
2223

2324
import org.junit.jupiter.api.BeforeEach;
@@ -34,6 +35,7 @@
3435
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3536
import static org.junit.jupiter.api.Assertions.assertEquals;
3637
import static org.junit.jupiter.api.Assertions.assertFalse;
38+
import static org.junit.jupiter.api.Assertions.assertNull;
3739
import static org.junit.jupiter.api.Assertions.assertThrows;
3840
import static org.junit.jupiter.api.Assertions.assertTrue;
3941
import static org.mockito.ArgumentMatchers.any;
@@ -347,4 +349,18 @@ void testSortThrowsExceptionWhenInvokedWithNull() {
347349
.withMessage("Sorts must not be null");
348350
}
349351

352+
@Test
353+
void testClose() throws Exception {
354+
// given
355+
when(template.find(any(), any())).thenReturn(List.of("string"));
356+
reader.read();
357+
358+
// when
359+
reader.close();
360+
361+
// then
362+
assertEquals(0, reader.page);
363+
assertNull(reader.results);
364+
}
365+
350366
}

0 commit comments

Comments
 (0)