Skip to content

Commit 2c97974

Browse files
Robert McNeesfmbenhassine
Robert McNees
authored andcommitted
Allow access to update counts in JdbcBatchItemWriter
Resolves #3829
1 parent 73beb00 commit 2c97974

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Diff for: spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcBatchItemWriter.java

+10
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,17 @@ public void write(final Chunk<? extends T> chunk) throws Exception {
207207
}
208208
}
209209
}
210+
211+
processUpdateCounts(updateCounts);
210212
}
211213
}
212214

215+
/**
216+
* Extension point to post process the update counts for each item.
217+
* @param updateCounts the array of update counts for each item
218+
*/
219+
protected void processUpdateCounts(int[] updateCounts) {
220+
// No Op
221+
}
222+
213223
}

Diff for: spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/JdbcBatchItemWriterClassicTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.junit.jupiter.api.BeforeEach;
2424
import org.junit.jupiter.api.Test;
25+
import org.mockito.Mockito;
2526

2627
import org.springframework.batch.item.Chunk;
2728
import org.springframework.dao.DataAccessException;
@@ -35,6 +36,7 @@
3536
import static org.junit.jupiter.api.Assertions.assertThrows;
3637
import static org.junit.jupiter.api.Assertions.assertTrue;
3738
import static org.mockito.Mockito.mock;
39+
import static org.mockito.Mockito.spy;
3840
import static org.mockito.Mockito.when;
3941

4042
/**
@@ -139,4 +141,23 @@ void testWriteAndFlushWithFailure() throws Exception {
139141
assertTrue(list.contains("foo"));
140142
}
141143

144+
@Test
145+
void testProcessUpdateCountsIsCalled() throws Exception {
146+
JdbcBatchItemWriter<String> customWriter = spy(new JdbcBatchItemWriter<>());
147+
148+
customWriter.setSql("SQL");
149+
customWriter.setJdbcTemplate(new NamedParameterJdbcTemplate(jdbcTemplate));
150+
customWriter.setItemPreparedStatementSetter((item, ps) -> list.add(item));
151+
customWriter.afterPropertiesSet();
152+
153+
ps.addBatch();
154+
int[] updateCounts = { 123 };
155+
when(ps.executeBatch()).thenReturn(updateCounts);
156+
customWriter.write(Chunk.of("bar"));
157+
assertEquals(2, list.size());
158+
assertTrue(list.contains("SQL"));
159+
160+
Mockito.verify(customWriter, Mockito.times(1)).processUpdateCounts(updateCounts);
161+
}
162+
142163
}

0 commit comments

Comments
 (0)