knav_negi

knav_negi

Optimistic locking inside a transaction (Ecto.Multi)

In our application we have a code like below in Elixir:

      Multi.new()
  |> Multi.update(
    :some_operation_name,
    Entity
    |> Repo.preload(:some_entity)
    |> Ecto.Changeset.optimistic_lock(:lock_version)
    |> Ecto.Changeset.change(some_id: some_uuid)
  )
   |> Repo.transaction()

We are using MYSql database, I find this code a little bit strange because it is using optimistic_locking inside a transation,I am wondering if we might run into issues because of using optimistic_locking inside a transction (pessimistic locking).

First Post!

kip

kip

ex_cldr Core Team

A transaction essentially means “everything succeeds or nothing succeeds”, and although locking plays a part in determine the success or otherwise many other issues come into play: access control, timeouts, constraints and so on.

So here the optimistic lock may or may not succeed whether a lock of some kind is acquired or not. But if the optimistic lock fails, you want all of the multi steps to rollback, which is what the transaction will accomplish.

MySQL uses REPEATABLE_READ as the default isolation level so another transaction may have updated the optmistic lock column while the current transaction is in process. Therefore the optimistic lock will fail and the transaction would roll back.

Where Next?

Popular in Discussions Top

scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
AstonJ
I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ec...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
pillaiindu
I want to convert a Phoenix LiveView CRUD website to a CRUD mobile app. What do you think is the easiest way to do so?
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement