UpdatableRecord merge not supported with optimistic locking enabled
Author: Paolo-de-vathaireCreated Apr 3, 2025Updated Sep 10, 2026
LabelsT: DefectC: FunctionalityP: MediumE: All Editions
Expected behavior
CREATE TABLE public.customer_settings (
customer_id text NOT NULL,
key text NOT NULL,
value text NOT NULL
);
ALTER TABLE ONLY public.customer_settings
ADD CONSTRAINT customer_settings_pkey PRIMARY KEY (customer_id, key);dslContext.configuration().settings().setExecuteWithOptimisticLocking(true);
var settingRecord = dslContext.newRecord(CUSTOMER_SETTINGS).
setCustomerId("existingCustomerId")
.setKey("existingKey")
.setValue("newValue");
settingRecord.merge();Should generate
insert into "public"."customer_settings" ("customer_id", "key", "value")
values (?, ?, ?)
on conflict ("customer_id", "key")
do update set "value" = excluded."value"`Actual behavior
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "customer_settings_pkey"
Detail: Key (customer_id,key)=(existingCustomerId, existingKey) already exists.Steps to reproduce the problem
CREATE TABLE public.customer_settings (
customer_id text NOT NULL,
key text NOT NULL,
value text NOT NULL
);
ALTER TABLE ONLY public.customer_settings
ADD CONSTRAINT customer_settings_pkey PRIMARY KEY (customer_id, key);dslContext.configuration().settings().setExecuteWithOptimisticLocking(true);
var settingRecord = dslContext.newRecord(CUSTOMER_SETTINGS)
.setCustomerId("existingCustomerId")
.setKey("existingKey")
.setValue("newValue");
settingRecord.merge();This generates a simple insert statement without on conflict clause. This causes duplicate key error when the values already exists.
It can be worked around using explicit onDuplicateKeyUpdate() but this is not available for dsl.batchMerge(). In this case the only workaround seems to be disabling the optimistic locking for this operation
jOOQ Version
JOOQ communty 3.19.18
Database product and version
Postgresql 15
Java Version
openjdk 21
JDBC / R2DBC driver name and version (include name if unofficial driver)
spring-jdbc 6.1.16
Source: jOOQ/jOOQ