Reputation: 14539
How do I atomically check-and-set something in Ecto Repo? I want to make sure no other process changed any part of the struct in parallel, even if the two writes are not-overlapping and read the same data.
For example,
Process A:
1. read {x=3, y=5}
2. update {x=100+3+5}
Process B:
1. read {x=3, y=5}
2. update {y=200+3+5}
Here at least one of the processes (and preferably exactly one) should fail and retry, since they both depend on both values, even if they only change one.
Upvotes: 0
Views: 89
Reputation: 2233
Ecto.Changeset.optimistic_lock/3 does exactly what you want. It will require a migration to the table in question (adds a version
field or whatever you want to call it).
I could give you an example but I would just be copying and pasting from the docs. They describe it perfectly.
Upvotes: 1