Optimistic Locking and DAOs does not work

57 views
Skip to first unread message

Simon Martinelli

unread,
May 17, 2023, 10:39:29 AM5/17/23
to jOOQ User Group
Hi,

I've configured optimistic locking:

@Configuration
public class SakilaJooqConfiguration {

@Bean
public DefaultConfigurationCustomizer configurationCustomizer() {
    return (DefaultConfiguration c) -> c.settings()
        .withExecuteWithOptimisticLocking(true);
    }
}

Now I try to use a DAO with a POJO to read and update:

var rentalDao = new RentalDao(dsl.configuration());

rentalDao.findOptionalById(854L).ifPresent(rental -> {
    rental.setRentalDate(LocalDateTime.now());
    rentalDao.update(rental);
});

But this leads to 


org.jooq.exception.DataChangedException: Database record has been changed

at org.jooq.impl.UpdatableRecordImpl.checkIfChanged(UpdatableRecordImpl.java:507)
at org.jooq.impl.UpdatableRecordImpl.storeMergeOrUpdate0(UpdatableRecordImpl.java:345)
at org.jooq.impl.UpdatableRecordImpl.storeUpdate0(UpdatableRecordImpl.java:240)
at org.jooq.impl.UpdatableRecordImpl.lambda$storeUpdate$1(UpdatableRecordImpl.java:232)
at org.jooq.impl.RecordDelegate.operate(RecordDelegate.java:144)
at org.jooq.impl.UpdatableRecordImpl.storeUpdate(UpdatableRecordImpl.java:231)
at org.jooq.impl.UpdatableRecordImpl.update(UpdatableRecordImpl.java:168)
at org.jooq.impl.UpdatableRecordImpl.update(UpdatableRecordImpl.java:163)
at org.jooq.impl.DAOImpl.update(DAOImpl.java:210)
at org.jooq.impl.DAOImpl.update(DAOImpl.java:186)

When I debug into UpdatableRecordImpl I see that in the record only the id is set.

Is this a bug or did I miss somehting?

Thanks, 
Simon

Lukas Eder

unread,
May 17, 2023, 10:53:50 AM5/17/23
to jooq...@googlegroups.com
Hi Simon,

What column(s) did you configure for your optimistic locking checks? What SQL queries are being generated in your debug logs?

Best Regards,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/b734e1bf-0121-47a2-be39-196b0cd8b1e5n%40googlegroups.com.

Simon Martinelli

unread,
May 17, 2023, 11:06:04 AM5/17/23
to jOOQ User Group
I didn't configure any column so it should use all columns.

(Make sure Docker is running locally)

That's the test that causes the problem:

Lukas Eder

unread,
May 17, 2023, 11:27:26 AM5/17/23
to jooq...@googlegroups.com
Thanks, I will, soon

Lukas Eder

unread,
May 24, 2023, 8:21:46 AM5/24/23
to jooq...@googlegroups.com
Thanks for your patience. I've had a look at this issue and created a ticket here with some more details:

I don't think jOOQ can do much about this case. jOOQ doesn't know which fields you've changed in your POJO (rental.setRentalDate()) and which fields have been changed in the database, because unlike the UpdatableRecord, the POJO doesn't keep any dirty flags. That's the point of the POJO types. However, there might be an argument in favour of ignoring the optimistic locking feature when:

- An UpdatableRecord was not "fetched" (i.e. created entirely in the client, e.g. from a POJO as in your case)
- When we're using unversioned optimistic locking (because with versioned rows, this problem wouldn't happen)

Thoughts?

Lukas Eder

unread,
May 24, 2023, 8:22:52 AM5/24/23
to jooq...@googlegroups.com
... on second thought, I don't think we should ignore the feature and store the record silently. Much rather, a different error message ("can't lock this particular record") would help explain the problem.

Simon Martinelli

unread,
May 27, 2023, 2:58:39 AM5/27/23
to jOOQ User Group
Hi Lukas,

Thanks for the explanation.
Yes, I think a more expressive error message would be helpful.

Thanks, Simon

Lukas Eder

unread,
May 30, 2023, 4:03:41 AM5/30/23
to jOOQ User Group
https://github.com/jOOQ/jOOQ/issues/15115 (an improved error message and javadoc on the DataChangedException) has been implemented for 3.19.0, 3.18.5, 3.17.14, 3.16.20
Reply all
Reply to author
Forward
0 new messages