Skip to content

Commit 6fcbc25

Browse files
committed
Use batch inserts for job parameters
Use batch inserts for job parameters to reduce the number of database round trips.
1 parent 3fbfbb9 commit 6fcbc25

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2019 the original author or authors.
2+
* Copyright 2006-2021 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.
@@ -16,15 +16,16 @@
1616

1717
package org.springframework.batch.core.repository.dao;
1818

19+
import java.sql.PreparedStatement;
1920
import java.sql.ResultSet;
2021
import java.sql.SQLException;
2122
import java.sql.Timestamp;
2223
import java.sql.Types;
2324
import java.util.HashMap;
2425
import java.util.HashSet;
2526
import java.util.List;
27+
import java.util.Date;
2628
import java.util.Map;
27-
import java.util.Map.Entry;
2829
import java.util.Set;
2930

3031
import org.apache.commons.logging.Log;
@@ -61,6 +62,7 @@
6162
* @author Michael Minella
6263
* @author Mahmoud Ben Hassine
6364
* @author Dimitrios Liapis
65+
* @author Philippe Marschall
6466
*/
6567
public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements JobExecutionDao, InitializingBean {
6668

@@ -321,42 +323,53 @@ public void synchronizeStatus(JobExecution jobExecution) {
321323
*/
322324
private void insertJobParameters(Long executionId, JobParameters jobParameters) {
323325

324-
for (Entry<String, JobParameter> entry : jobParameters.getParameters()
325-
.entrySet()) {
326-
JobParameter jobParameter = entry.getValue();
327-
insertParameter(executionId, jobParameter.getType(), entry.getKey(),
328-
jobParameter.getValue(), jobParameter.isIdentifying());
326+
if (jobParameters.isEmpty()) {
327+
return;
329328
}
330-
}
331329

330+
getJdbcTemplate().batchUpdate(getQuery(CREATE_JOB_PARAMETERS), jobParameters.getParameters().entrySet(), 100, (ps, entry) -> {
331+
JobParameter jobParameter = entry.getValue();
332+
String key = entry.getKey();
333+
ParameterType type = jobParameter.getType();
334+
Object value = jobParameter.getValue();
335+
boolean identifying = jobParameter.isIdentifying();
336+
setJobParameters(executionId, type, key, value, identifying, ps);
337+
});
338+
}
339+
332340
/**
333341
* Convenience method that inserts an individual records into the
334342
* JobParameters table.
335343
*/
336-
private void insertParameter(Long executionId, ParameterType type, String key,
337-
Object value, boolean identifying) {
338-
339-
Object[] args = new Object[0];
340-
int[] argTypes = new int[] { Types.BIGINT, Types.VARCHAR,
341-
Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.BIGINT,
342-
Types.DOUBLE, Types.CHAR };
343-
344-
String identifyingFlag = identifying? "Y":"N";
345-
344+
private static void setJobParameters(Long executionId, ParameterType type, String key,
345+
Object value, boolean identifying, PreparedStatement preparedStatement)
346+
throws SQLException {
347+
348+
preparedStatement.setLong(1, executionId);
349+
preparedStatement.setString(2, key);
350+
preparedStatement.setString(3, type.toString());
346351
if (type == ParameterType.STRING) {
347-
args = new Object[] { executionId, key, type, value, new Timestamp(0L),
348-
0L, 0D, identifyingFlag};
349-
} else if (type == ParameterType.LONG) {
350-
args = new Object[] { executionId, key, type, "", new Timestamp(0L),
351-
value, new Double(0), identifyingFlag};
352-
} else if (type == ParameterType.DOUBLE) {
353-
args = new Object[] { executionId, key, type, "", new Timestamp(0L), 0L,
354-
value, identifyingFlag};
355-
} else if (type == ParameterType.DATE) {
356-
args = new Object[] { executionId, key, type, "", value, 0L, 0D, identifyingFlag};
352+
preparedStatement.setString(4, (String) value);
353+
} else {
354+
preparedStatement.setString(4, "");
357355
}
358-
359-
getJdbcTemplate().update(getQuery(CREATE_JOB_PARAMETERS), args, argTypes);
356+
if (type == ParameterType.DATE) {
357+
preparedStatement.setTimestamp(5, new Timestamp(((Date) value).getTime()));
358+
} else {
359+
preparedStatement.setTimestamp(5, new Timestamp(0L));
360+
}
361+
if (type == ParameterType.LONG) {
362+
preparedStatement.setLong(6, (Long) value);
363+
} else {
364+
preparedStatement.setLong(6, 0L);
365+
}
366+
if (type == ParameterType.DOUBLE) {
367+
preparedStatement.setDouble(7, (Double) value);
368+
} else {
369+
preparedStatement.setDouble(7, 0.0d);
370+
}
371+
String identifyingFlag = identifying ? "Y" : "N";
372+
preparedStatement.setString(8, identifyingFlag);
360373
}
361374

362375
/**

0 commit comments

Comments
 (0)