Description
I have a trigger that inserts into sku_price_history table after inserting into sku table:
DELIMITER $$
CREATE TRIGGER trig_after_sku_insert
AFTER INSERT
ON sku FOR EACH ROW
BEGIN
INSERT INTO sku_price_history(sku_id, action_type, purchase_price, selling_price, base_margin_rate,
vat, vat_type, tax_type, selling_price_started_at, updated_by)
VALUES (NEW.id, 'CREATE', NEW.purchase_price, NEW.selling_price, NEW.base_margin_rate,
NEW.vat, NEW.vat_type, NEW.tax_type, NOW(), NEW.created_by);
END $$
DELIMITER ;
Then, I am trying to do a simple batch insert using jOOQ's DSLContext.batchInsert():
void saveMany(List<SkuDto.SaveBody> skus) {
List<SkuRecord> skuRecords = new ArrayList<>();
for(SkuDto.SaveBody sku : skus) {
SkuRecord skuRecord = ctx.newRecord(SKU, sku);
skuRecords.add(skuRecord);
}
ctx.batchInsert(skuRecords).execute();
}
Problem
Above statement inserts only the first row into sku table even though there are many records. No exception is thrown and batch size is correct according to LoggerListener:
DEBUG 65174 --- [nio-9292-exec-1] org.jooq.tools.LoggerListener : Executing batch query : sql query
DEBUG 65174 --- [nio-9292-exec-1] org.jooq.tools.LoggerListener : Batch size : 3
What I tried
- I executed the same code after dropping the trigger and it worked. So the trigger seems to be a main reason here.
- Then, I tried to simulate the problem using an SQL client, run multiple insert statements in transaction:
SET autocommit = OFF;
START TRANSACTION;
INSERT INTO sku (fields) VALUES (values);
INSERT INTO sku (fields) VALUES (values);
INSERT INTO sku (fields) VALUES (values);
COMMIT;
Above SQL statement worked as expected, records were inserted into both sku and sku_price_history tables.
I may be missing some points about triggers and batch insert but I couldn't find relevant information. I am using Spring Boot with default configurations.
Versions:
- jOOQ: 3.18.2
- Java: 17
- Spring Boot: 3.0.5
- Database (include vendor): MariaDB 10.6 (RDS)
PreparedStatement.addBatch()andPreparedStatement.executeBatch())?3.0.10(org.mariadb.jdbc:mariadb-java-client:3.0.10). I reproduced the problem usingPreparedStatementdirectly as you explained, the results are the same:executeBatch()inserts only 1 row without any exceptions when there is a trigger, if I drop the trigger and execute batch all the rows are inserted correctly.3.1.4)? Perhaps you can report this here (ideally with the JDBC reproducer, removing jOOQ if it's not related to jOOQ)? github.com/mariadb-corporation/mariadb-connector-j