rethinkdb_ecto icon indicating copy to clipboard operation
rethinkdb_ecto copied to clipboard

Support for optimistic locking?

Open Lida opened this issue 10 years ago • 2 comments

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?

Lida avatar Apr 26 '16 20:04 Lida

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.

almightycouch avatar Apr 28 '16 12:04 almightycouch

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))

Lida avatar May 01 '16 05:05 Lida