-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FirstOrCreate is not atomic #6253
Comments
All composite APIs are not atomic, you need to lock yourself (in fact, we can't lock, because when you have multiple services, you need distributed locks). Gorm uses Session to provide thread-safe access, I don't see thread-safety from your example, can you reproduce it? https://gorm.io/docs/v2_release_note.html#Method-Chain-Safety-x2F-Goroutine-Safety |
added it, but also fail. |
This is still an atomicity-related test, and has nothing to do with thread safety. Thread safety means that no data race will occur. |
Yes, I think the only way to solve this without doing atomic operations is to use a mutex lock. |
Yes |
ok, I will close it. |
GORM Playground Link
go-gorm/playground#590
Description
I tried running FirstOrCreate in multiple threads and got the following error output:
It seems that FirstOrCreate's operation on the database is not atomic and not thread-safe.
Expected: no errors and tests pass.
The text was updated successfully, but these errors were encountered: