diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcCursorItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcCursorItemReader.java index 5af63faff..39959afcb 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcCursorItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcCursorItemReader.java @@ -230,6 +230,7 @@ public class JdbcCursorItemReader extends ExecutionContextUserSupport implements JdbcUtils.closeStatement(this.preparedStatement); JdbcUtils.closeConnection(this.con); bufferredReader = null; + rs = null; } /* @@ -343,7 +344,7 @@ public class JdbcCursorItemReader extends ExecutionContextUserSupport implements */ public void open(ExecutionContext context) { Assert.state(!initialized, "Stream is already initialized. Close before re-opening."); - Assert.isNull(rs); + Assert.isNull(rs, "ResultSet still open! Close before re-opening."); Assert.notNull(context, "ExecutionContext must not be null"); executeQuery(); initialized = true; diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/CommonItemStreamItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/CommonItemStreamItemReaderTests.java index 339d7bee4..c4c7a6c6b 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/CommonItemStreamItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/CommonItemStreamItemReaderTests.java @@ -1,59 +1,79 @@ -package org.springframework.batch.item; - -import org.springframework.batch.item.sample.Foo; - -/** - * Common tests for readers implementing both {@link ItemReader} and - * {@link ItemStream}. Expected input is five {@link Foo} objects with values 1 - * to 5. - */ -public abstract class CommonItemStreamItemReaderTests extends CommonItemReaderTests { - - protected ExecutionContext executionContext = new ExecutionContext(); - - /** - * Cast the reader to ItemStream. - */ - protected ItemStream testedAsStream() { - return (ItemStream) tested; - } - - protected void setUp() throws Exception { - super.setUp(); - testedAsStream().open(executionContext); - } - - protected void tearDown() throws Exception { - super.tearDown(); - testedAsStream().close(executionContext); - } - - - - /** - * Restart scenario - read items, update execution context, create new - * reader and restore from restart data - the new input source should - * continue where the old one finished. - */ - public void testRestart() throws Exception { - - testedAsStream().update(executionContext); - - Foo foo1 = (Foo) tested.read(); - assertEquals(1, foo1.getValue()); - - Foo foo2 = (Foo) tested.read(); - assertEquals(2, foo2.getValue()); - - testedAsStream().update(executionContext); - - // create new input source - tested = getItemReader(); - - testedAsStream().open(executionContext); - - Foo fooAfterRestart = (Foo) tested.read(); - assertEquals(3, fooAfterRestart.getValue()); - } - -} +package org.springframework.batch.item; + +import org.springframework.batch.item.sample.Foo; + +/** + * Common tests for readers implementing both {@link ItemReader} and + * {@link ItemStream}. Expected input is five {@link Foo} objects with values 1 + * to 5. + */ +public abstract class CommonItemStreamItemReaderTests extends CommonItemReaderTests { + + protected ExecutionContext executionContext = new ExecutionContext(); + + /** + * Cast the reader to ItemStream. + */ + protected ItemStream testedAsStream() { + return (ItemStream) tested; + } + + protected void setUp() throws Exception { + super.setUp(); + testedAsStream().open(executionContext); + } + + protected void tearDown() throws Exception { + super.tearDown(); + testedAsStream().close(executionContext); + } + + + + /** + * Restart scenario - read items, update execution context, create new + * reader and restore from restart data - the new input source should + * continue where the old one finished. + */ + public void testRestart() throws Exception { + + testedAsStream().update(executionContext); + + Foo foo1 = (Foo) tested.read(); + assertEquals(1, foo1.getValue()); + + Foo foo2 = (Foo) tested.read(); + assertEquals(2, foo2.getValue()); + + testedAsStream().update(executionContext); + + // create new input source + tested = getItemReader(); + + testedAsStream().open(executionContext); + + Foo fooAfterRestart = (Foo) tested.read(); + assertEquals(3, fooAfterRestart.getValue()); + } + + public void testReopen() throws Exception { + testedAsStream().update(executionContext); + + Foo foo1 = (Foo) tested.read(); + assertEquals(1, foo1.getValue()); + + Foo foo2 = (Foo) tested.read(); + assertEquals(2, foo2.getValue()); + + testedAsStream().update(executionContext); + + // create new input source + testedAsStream().close(executionContext); + + testedAsStream().open(executionContext); + + Foo fooAfterRestart = (Foo) tested.read(); + assertEquals(3, fooAfterRestart.getValue()); + } + +}