Optimistic locking on delete: appendOptimisticLockingAttributes calls getRetainedSnapshot() but may be null for simple deleteObject()

From: Mike Kienenberger (mkienen..laska.net)
Date: Wed Feb 16 2005 - 22:08:52 EST

  • Next message: Mike Kienenberger: "ContextCommit.categorizeFlattenedDeletesAndCreateBatches and optimistic locking"

    Hey Andrus,

    I'm trying to get optimistic locking working on deletes, but my unit tests
    are currently failing on a simple case.

            context.commitChanges();
            context.deleteObject(object);
            context.commitChanges(); // fails on second delete

    java.lang.NullPointerException
            at
    org.objectstyle.cayenne.access.ContextCommit.appendOptimisticLockingAttributes(ContextCommit.java:599)

    This appears to be because appendOptimisticLockingAttributes() sets up the
    qualifier snapshot from the retained ObjectStore snapshot.
    I suppose an updated object is always guaranteed to have a retained
    snapshot, but if all that's going on is a delete, there's no retained
    snapshot since it's been cleared by the previous commit.

    It looks like the solution is to use getCachedSnapshot() instead (which
    first returns getRetainedSnapshot() if available).
    Does this make sense?

    Once I make this change, cayenne passes all of the unit tests, but new and
    old.

    -Mike

        private void appendOptimisticLockingAttributes(
                Map qualifierSnapshot,
                DataObject dataObject,
                List qualifierAttributes) throws CayenneException {

            Map snapshot =
    dataObject.getDataContext().getObjectStore().getRetainedSnapshot(
                    dataObject.getObjectId());

            Iterator it = qualifierAttributes.iterator();
            while (it.hasNext()) {
                DbAttribute attribute = (DbAttribute) it.next();
                String name = attribute.getName();
                if (!qualifierSnapshot.containsKey(name)) {
                    qualifierSnapshot.put(name, snapshot.get(name)); //
    ContextCommit.java:599 -- snapshot = null
                }
            }
        }



    This archive was generated by hypermail 2.0.0 : Wed Feb 16 2005 - 22:08:03 EST