1

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

  1. I executed the same code after dropping the trigger and it worked. So the trigger seems to be a main reason here.
  2. 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)
6
  • 1
    What JDBC driver and version are you using? Can you reproduce the problem using JDBC directly (using PreparedStatement.addBatch() and PreparedStatement.executeBatch())?
    – Lukas Eder
    Jun 8, 2023 at 13:05
  • I am using MariaDB JDBC driver version 3.0.10 (org.mariadb.jdbc:mariadb-java-client:3.0.10). I reproduced the problem using PreparedStatement directly 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. Jun 9, 2023 at 8:01
  • Maybe I should try with other JDBC drivers Jun 9, 2023 at 8:03
  • 1
    I'd use the official driver. Does the issue reproduce with the latest version (currently 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
    – Lukas Eder
    Jun 9, 2023 at 8:18
  • 1
    Yes it's the official one. I just replied to your idea of "I should try iwth other JDBC drivers". I wouldn't use a non-official driver.
    – Lukas Eder
    Jun 9, 2023 at 9:39

1 Answer 1

1

After helpful guidance from Lukas Eder, I've discovered the issue I was experiencing is not a bug with jOOQ itself but with the MariaDB JDBC driver. I am documenting steps to take before asking jOOQ related questions for the sake of public knowledge. It may assist others(or myself) who encounter similar problem in the future.

  1. Check for side effects when you think jOOQ is not working as expected. Side effects can be anything from application level VisitListeners to database level triggers. I spent hours trying to solve the problem until I found it was because of a trigger.
  2. Reproduce the problem using JDBC directly. If the same problem occurs then most likely it's not the problem with the jOOQ
  3. It's always best to use the official driver but for the sake of identifying the problem try switching the JDBC driver if you can. For example from MariaDB Connector/J to MySQL Connector/J.

I followed above steps and resolved the problem after switching from MariaDB Connector/J to MySQL Connector/J. This confirms that the issue is not with jOOQ itself, but with the specific MariaDB JDBC driver I was using. I think I can continue using MySQL driver since it's compatible with the JDBC 4.2 specification according to the documentation.

MySQL Connector/J 8.0 is a JDBC Type 4 driver that is compatible with the JDBC 4.2 specification.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.