← all projects

silverstripe-optimisticlocking

Prevent overwriting data by blocking the save process when the data changed since retrieval (using a timestamp)
repo

silverstripe-optimisticlocking

This is a very simple module that prevents your site users from losing data. It works by blocking the save process if the data changes since it's loaded. (user1 starts editing, user 2 starts editing, user1 saves, now user2 can't overwrite user1's changes). By default, Silverstripe lets you lose data by overwriting whatever is in the database on save .

Usage:

  1. Install using composer composer require "svandragt/silverstripe-optimisticlocking:*"

  2. Attach the OptimisticLocking class to your data objects / page types, eg. Test DataObject:

    Object::add_extension("Test","OptimisticLocking");

I keep a list of known bugs.

What is Optimistic / Pessimistic locking?

These are methodologies used to handle multi-user issues. How does one handle the fact that 2 people want to update the same record at the same time?

1.Do Nothing

User 2 has now over-written the changes that User 1 made. They are completely gone, as if they never happened. This is called a 'lost update' and is how SilverStripe works by default.

2.Lock the record when it is read: Pessimistic locking

The lost update problem is solved. The problem with this approach is concurrency. User 1 is locking a record that they might not ever update. User 2 cannot even read the record because they want an exclusive lock when reading as well. This approach requires far too much exclusive locking, and the locks live far too long (often across user control - an absolute no-no). This approach is almost never implemented.

3. Use Optimistic Locking.
Optimistic locking does not use exclusive locks when reading. Instead, a check is made during the update to make sure that the record has not been changed since it was read. This module implements this.

Thanks to chrisrlong for the original explanation.