Welcome to the new Gigaspaces XAP forum. To recover your account, please follow these instructions.

Ask Your Question

Optimistic locking on deletes?


I am starting to make use of optimistic locking, which is working OK for updates.

However I've come to use it for deletes, but it doesn't seem to be working?

Specifically, using GigaSpace.clear() with a Pojo whose @SpaceVersion field is out-of-date doesn't seem to do the version checking?

I would expect this test to pass, but it never throws a SpaceOptimisticLockingFailureException.

@Test(expected = SpaceOptimisticLockingFailureException.class)
public void testLockingViaPojoDelete() {
    final Integer ID = 1;

    // Version is managed by GigaSpace, and is 1 after write.
    gigaSpace.write(new MyPojo(ID).setFirstName("Rob").setSurname("Smith"), WriteModifiers.WRITE_ONLY);
    assertThat(gigaSpace.read(new MyPojo(ID)).getVersion(), is(1));

    // We're allowed to delete if our version is current.
    MyPojo pojo1 = new MyPojo(ID);

    assertNull(gigaSpace.read(new MyPojo(ID)));

    // Re-insert, edit to bump version to 2, and try to delete when version is stale.
    gigaSpace.write(new MyPojo(ID).setFirstName("Rob").setSurname("Smith"), WriteModifiers.WRITE_ONLY);
    gigaSpace.write(new MyPojo(ID).setFirstName("Robert"), WriteModifiers.PARTIAL_UPDATE);
    assertThat(gigaSpace.read(new MyPojo(ID)).getVersion(), is(2));

    pojo1 = new MyPojo(ID);

If I change that last line to "gigaSpace.write(pojo1)", thereby turning the delete into an update, then version checking is applied, and the test passes, as a SpaceOptimisticLockingFailureException.

So the question being is optimistic locking implemented for deletes? If not, what are my options for checking the version on a delete?


{quote}This thread was imported from the previous forum. For your reference, the original is [available here|http://forum.openspaces.org/thread.jspa?threadID=3894]{quote}

asked 2012-08-03 03:19:32 -0500

awarner gravatar image

updated 2013-08-08 09:52:00 -0500

jaissefsfex gravatar image
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

Optimistic locking designed to avoid data inconsistently on data updates. I'm not sure it is supported on data removal.

Here is a simple solution - implement a remoting service/task that will check the current version and clear it if the existing version passed from the client matches. The read will be extremely fast. You can mak this entire operation transactional to ensure atomicy.

Would this work for you?


answered 2012-08-03 08:26:42 -0500

shay hassidim gravatar image
edit flag offensive delete link more



I think it's probably an omission then if it's not supported for deletions. I'll open a support ticket to get final confirmation that it doesn't exist, perhaps it can be added, since I think it's perfectly valid to prevent deletion of stale data.

Yes, I had thought of that work-around, I might consider it. So far I created a work-around that uses JDBC/SQL, which works OK too.

awarner gravatar imageawarner ( 2012-08-03 08:33:27 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2012-08-03 03:19:32 -0500

Seen: 54 times

Last updated: Aug 03 '12