Hi Sergey
> In this case "balance" column can be changed using direct update statement
> factory.update(Reseller).set(balance, <new value>) etc..
> (still not sure that we need special API for optimistic locking).
(note, everything said about store() is true for delete() as well...)
I see, so according to you, backend jobs can perform "forced" updates
through explicit usage of the Factory's update() methods, whereas
client code operating on records will always perform optimistic
locking. To be consistent, however, this would mean that storeLocked()
would be removed again, and store() would implement optimistic locking
no matter if "timestamp" / "version" columns have been defined (i.e.
always assuming today's storeLocked() behaviour).
I'm afraid that this might break existing jOOQ client code... Yet, I
understand that 80% of all calls to store() align well with optimistic
locking.
An option would be to add a new Settings property, indicating whether
store() should perform optimistic locking in the absence of a
"timestamp" / "version" column. For backwards compatibility, this
property is set to "false" by default.
> It's well known pattern, when entity has "audit" fields (CREATED_TS,
> CREATED_BY, UPDATED_TS, UPDATED_BY).
> These fields contain information when entity created/updated and who is
> author of these changes.
>
> Not sure, may be these ideas can be combined in one extension point, that
> allows different strategies: optimistic locking waving and updating audit
> fields.
Yes, that is true. This idea was seen on the user group before in this thread:
https://groups.google.com/d/topic/jooq-user/iS3UBATSorA/discussion
And in the thread that originally lead to me implementing optimistic
locking support:
https://groups.google.com/d/topic/jooq-user/bGUYxeNd5X0/discussion
While I'm inclined to say that this is something quite
application-specific, I can imagine that this would be useful to a
great many jOOQ users. Hence, I will track this now as #1592:
https://sourceforge.net/apps/trac/jooq/ticket/1592
So in essence, the following can be said:
- Many tables have a "version" / "timestamp" field used for optimistic locking.
- Many tables have DC, UC, DM, UM fields (D=Date, U=User, C=Creation,
M=Modification)
- NVL(DC, DM) often coincides with "timestamp"
- UpdatableTable would allow to formally access all of these fields.
This access can be configured and generated in jooq-codegen
- If "version" or "timestamp" are provided, store() and delete() would
check for their integrity before actually performing an UPDATE/DELETE
- If DC/DM are provided, these values are set with every INSERT or
UPDATE respectively. Either, System.currentTimeMillis() is used, or
Factory.currentTimestamp()
- If UC/UM are provided, these values are set according to some
configurable Field<String> (e.g. val("Lukas"), or
Factory.currentUser()). I'm not sure yet how to configure this.
Note: Any of the above can also be set using database triggers,
though. Instead of a DataChangedException, triggers would generate a
regular DataAccessException. In that case, jOOQ couldn't provide any
help to users.
I like where this is going...
Cheers
Lukas