JDBC · Lesson 8
Batch Processing
Why Batch Processing Matters
Batch processing sends multiple related updates to the database together instead of issuing one round trip per row. It reduces network overhead and can improve throughput for imports, migrations, and scheduled jobs. Batches still need validation, transaction boundaries, and useful failure reporting.
Adding a Batch
Use addBatch() for each set of bound parameters and call executeBatch() once. The returned counts show how the driver handled each command.
import java.sql.Connection;
import java.sql.PreparedStatement;
public class ProgressImporter {
public int importScores(Connection connection, long[] learnerIds, int[] scores) throws Exception {
String sql = "UPDATE lesson_progress SET score = ? WHERE learner_id = ?";
int updatedRows = 0;
try (PreparedStatement statement = connection.prepareStatement(sql)) {
for (int index = 0; index < learnerIds.length; index++) {
statement.setInt(1, scores[index]);
statement.setLong(2, learnerIds[index]);
statement.addBatch();
}
for (int count : statement.executeBatch()) {
if (count > 0) updatedRows += count;
}
}
return updatedRows;
}
}
Batch Design Choices
| Choice | Small batch | Large batch |
|---|---|---|
| Memory use | Lower | Higher |
| Commit frequency | More frequent | Less frequent |
| Error isolation | Easier | More complex |
| Throughput | Moderate | Often higher |
Common Mistake and Practices
Common Mistake: Adding millions of records to one in-memory batch. The application can exhaust memory and make a single failure expensive to retry.
- Process bounded batches.
- Commit according to recovery requirements.
- Validate input before adding it.
- Record failed ranges or identifiers.
- Measure throughput with realistic data.
Real-World Use Case
A nightly job imports learner progress from an external file in batches of 500 rows. It commits each successful batch and records the source line numbers of failures for a targeted retry.
Quiz Check