问题
I wanted to do batch updates using Springs JDBC template in postgre.
However, I am curious that do I really need to use the jdbcTemplate.BatchUpdate() calls. I read in several places that they are slower if not implemented correctly.
Will adding a @Transactional on the method that inserts in a loop achieve the same functionality of batching the updates?
If the below method that inserts one record at a time, is called from a loop which is in a transactional as shown, will this cause updates to happen in a batched fashion?
public void insertProdPayload(String Prod_Name, LocalDate sourceDt, String exchangeId, int status, String payload) {
int ret = jdbcTemplate
.update(DBQueryConstants.INSERT_PAYLOAD, Prod_Name, sourceDt, exId, status, payload);
}
@Override
@Transactional
public void ingestListOfPayloads(List<Message<MessageMetricRecord>> payloadMessages) {
for (Message<Payload> message : payloadMessages) {
insertProdPayload(message.getProdName,message.getDate(),message.getExchangeID(),message.getStatus(),message.getPayload());
}
}
The jdbc template has default config of SpringBoot 2 and PostGreSql version is 9.6.
来源:https://stackoverflow.com/questions/65062510/will-transactional-cause-batch-updates-with-jdbctemplate-if-used-in-a-loop