After much flip-flopping, I finally have full-attribute optimistic locking
working for Batch Updating.
All of yesterday's ideas were bad ones. DataContext only handles select
queries, not update queries. Update Queries don't have attribute expression
parameters like select queries do. CreateSQLString no longer has access to
the DataObject current values.
What I have done is to modify ContextCommit's prepareUpdateQueries().
For each ObjEntity, I generate an alternate list of idDbAttributes for use
in BatchUpdateQuery. Basically, all of them rather than just the primary
keys. To add this to the BatchUpdateQuery, I had to add a public method to
reset idDbAttributes since it is hardcoded to use getPrimaryKey() in the
constructor.
Then, for each dataObject, I update the idSnapshot to contain the alternate
idDbAttributes fields and values.
This only requires minor code changes to
ContextCommit.prepareUpdateQueries() and adds a new method to
BatchUpdateQuery.
It also can trivially be modified to lock on only certain entities or fields
by implementing a couple of methods.
// Per-objEntity optimistic locking conditional
// boolean shouldUseOptimisticLocking =
objEntity.shouldUseOptimisticLocking();
boolean shouldUseOptimisticLocking = true;
and
// Per-objAtttribute optimistic locking conditional
// if (false == objAttr.useForOptimisticLock()) return;
I don't know if it's sufficient to change prepareUpdateQueries() for batched
and unbatched queries or if there's another path that update queries can
take through the code, but it looks like this is sufficient.
If what I've described sounds like a reasonable approach for implementing
optimistic locking in Cayenne, I will clean up my code and open an RFE with
patches.
If not, I'll probably just keep this as a local mod without pursuing it
further since I always want to optimistically lock on all fields and
entities in my own project.
-Mike
This archive was generated by hypermail 2.0.0 : Wed Feb 04 2004 - 15:13:08 EST