BATCH-549: JdbcCursorItemReader will not be support being restarted more than once without issue. Added an additional unit test for all item readers to make sure this case is tested for.
This commit is contained in:
@@ -325,12 +325,13 @@ public class JdbcCursorItemReader extends ExecutionContextUserSupport implements
|
||||
Assert.notNull(context, "ExecutionContext must not be null");
|
||||
executeQuery();
|
||||
initialized = true;
|
||||
long processedRowCount = 0;
|
||||
|
||||
if (context.containsKey(getKey(CURRENT_PROCESSED_ROW))) {
|
||||
try {
|
||||
long currentProcessedRow = context.getLong(getKey(CURRENT_PROCESSED_ROW));
|
||||
processedRowCount = context.getLong(getKey(CURRENT_PROCESSED_ROW));
|
||||
while(rs.next()){
|
||||
if(rs.getRow() == currentProcessedRow){
|
||||
if(rs.getRow() == processedRowCount){
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -339,7 +340,7 @@ public class JdbcCursorItemReader extends ExecutionContextUserSupport implements
|
||||
}
|
||||
}
|
||||
|
||||
bufferredReader = new BufferredResultSetReader(rs, mapper);
|
||||
bufferredReader = new BufferredResultSetReader(rs, mapper, processedRowCount);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -447,14 +448,18 @@ public class JdbcCursorItemReader extends ExecutionContextUserSupport implements
|
||||
private long processedRowCount;
|
||||
private int INITIAL_POSITION = -1;
|
||||
|
||||
public BufferredResultSetReader(ResultSet rs, RowMapper rowMapper) {
|
||||
public BufferredResultSetReader(ResultSet rs, RowMapper rowMapper, long processedRowCount) {
|
||||
Assert.notNull(rs, "The ResultSet must not be null");
|
||||
Assert.notNull(rowMapper, "The RowMapper must not be null");
|
||||
this.rs = rs;
|
||||
this.rowMapper = rowMapper;
|
||||
buffer = new ArrayList();
|
||||
currentIndex = INITIAL_POSITION;
|
||||
processedRowCount = 0;
|
||||
this.processedRowCount = processedRowCount;
|
||||
}
|
||||
|
||||
public BufferredResultSetReader(ResultSet rs, RowMapper rowMapper){
|
||||
this(rs, rowMapper, 0);
|
||||
}
|
||||
|
||||
public Object read() throws Exception, UnexpectedInputException,
|
||||
|
||||
@@ -191,6 +191,47 @@ public abstract class AbstractDataSourceItemReaderIntegrationTests extends
|
||||
assertEquals(foo2, reader.read());
|
||||
assertEquals(foo3, reader.read());
|
||||
}
|
||||
|
||||
public void testMultipleRestarts() throws Exception {
|
||||
|
||||
getAsItemStream(reader).open(executionContext);
|
||||
|
||||
Foo foo1 = (Foo) reader.read();
|
||||
|
||||
commit();
|
||||
|
||||
Foo foo2 = (Foo) reader.read();
|
||||
Assert.state(!foo2.equals(foo1));
|
||||
|
||||
Foo foo3 = (Foo) reader.read();
|
||||
Assert.state(!foo2.equals(foo3));
|
||||
|
||||
rollback();
|
||||
|
||||
getAsItemStream(reader).update(executionContext);
|
||||
|
||||
// create new input source
|
||||
reader = createItemReader();
|
||||
|
||||
getAsItemStream(reader).open(executionContext);
|
||||
|
||||
assertEquals(foo2, reader.read());
|
||||
assertEquals(foo3, reader.read());
|
||||
|
||||
getAsItemStream(reader).update(executionContext);
|
||||
|
||||
commit();
|
||||
|
||||
// create new input source
|
||||
reader = createItemReader();
|
||||
|
||||
getAsItemStream(reader).open(executionContext);
|
||||
|
||||
Foo foo4 = (Foo)reader.read();
|
||||
Foo foo5 = (Foo)reader.read();
|
||||
assertEquals(4, foo4.getValue());
|
||||
assertEquals(5, foo5.getValue());
|
||||
}
|
||||
|
||||
private void commit() {
|
||||
reader.mark();
|
||||
|
||||
@@ -25,5 +25,6 @@ public class JdbcCursorItemReaderIntegrationTests extends AbstractDataSourceItem
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user