snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
Atlas CI Public / lint (push) Failing after 13s
Atlas CI Public / sync (push) Has been skipped

This commit is contained in:
upstream-mirror
2026-09-15 22:12:36 +05:00
commit 4b3ad0b611
2680 changed files with 719472 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
# [Optimistic Lock](https://en.wikipedia.org/wiki/Optimistic_concurrency_control)
In this example, we implement an optimistic locking mechanism using the technique mentioned in
[Ent Blog](https://entgo.io/blog/2021/07/22/database-locking-techniques-with-ent/).
The idea is to add to our schema a `version` field that holds the Unix time of when the latest update occurred.
When an `Update` operation is executed, the hook updates the `version` field with the new value and adds a predicate
to verify that the `version` wasn't updated by another process/transaction during the mutation.
An error is returned if the versions are mismatched, and the user should reload the entity and retry the mutation.
### Generate Assets
```console
go generate ./...
```
### Run Example
```console
go test
```