Background
I want to implement the following procedure with Spring and MyBatis:
- Read a line from a CSV file
- Query a table (table
ACCOUNT) with values read from the CSV file. - Merge the values from the CSV file and table
ACCOUNTinto a record - Insert the merged record into another table (
REGISTRATION)
I want to use the batch executor (<setting name="defaultExecutorType" value="BATCH"/>) because this procedure inserts large number of records.
Problem
I have implemented the procedure like this:
AccountDao accountDao = applicationContext.getBean("accountDao", AccountDao.class);
RegistrationDao registrationDao = applicationContext.getBean("registrationDao", RegistrationDao.class);
List<Registration> list = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"))) {
CsvToBeanBuilder<Registration> builder = new CsvToBeanBuilder<Registration>(reader);
builder.withType(Registration.class);
list = builder.build().parse();
for (Registration reg : list) {
Account account = accountDao.findAccount(reg.getAccountId());
reg.setName(account.getName());
...
registrationDao.insertRegistration(reg);
}
}
I have expected the INSERT SQL to be queued with JDBC Statement#addBatch(), and executed in a batch at the end of the transaction.
But in actually, MyBatis automatically flush every statement with BatchExecutor#doFlushStatements() when I query ACCOUNT table with AccountDao#findAccount(), so all INSERT SQL are executed separately.
In this situation, ACCOUNT has no relation to REGISTRATION, so you can safely queue the update statements and execute them in a batch.
Question
Is there any way to suppress automated flushing of queued statements when the developer knows the queried tables have no relation to the updated tables?