Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.

Support for optimistic locking? #5

Open
Lida opened this issue Apr 26, 2016 · 2 comments
Open

Support for optimistic locking? #5

Lida opened this issue Apr 26, 2016 · 2 comments

Comments

@Lida
Copy link
Contributor

Lida commented Apr 26, 2016

I was running the example in Ecto's doc for optimistic locking while using RethinkDB.Ecto as the adapter.

Repo.update!(stale_change) didn't update the row, but it didn't throw a Ecto.StaleModelError either.

What is going on here?

@almightycouch
Copy link
Collaborator

Hi @Lida, thank you for pointing that out. Basically, RethinkDB has built-in support for atomic updates. You don't have to rely on Ecto's optimistic lock to allow lock-free updates on a single record.

I will investigate why the update did not work as excepted.

@Lida
Copy link
Contributor Author

Lida commented May 1, 2016

Hi @almightycouch, the optimistic locking is being used to make sure concurrent updates doesn't conflict. For example allowing multiple user to increment a counter. I am currently using ReQL directly to make sure we don't clobber any updates.

table("counters") |> get(model.id) |> update((lambda fn(row) ->
      if row["version"] == model.version do
        Map.merge(params, %{version: model.version + 1})
      else
        error("Can't update")
      end
    end))

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants