Hi, I’ve read these posts dealing with WIX DB atomicity/ transaction/ record locking.
https://www.wix.com/velo/forum/coding-with-velo/atomicity-when-updating-a-field-in-collection
https://www.wix.com/velo/forum/coding-with-velo/avoid-duplicates-in-database-in-case-of-concurrent-data-inserts
https://www.wix.com/velo/forum/community-discussion/solved-question-about-record-locking-and-one-user-overriding-another-s-data
All answered thoroughly by @giri-zano
My question is probably redundant, but I just would like to make sure my data is protected.
I’m using a set of collections to store new leads coming from a landing page on my website.
My scenario is:
User A: completes calculator and fills his contact details
User B does the same
Both submit the data at the same time (wix-data.save is called)
Will I have 2 records - one with user A data and the other with user B? or will I have a mix (as the data is written to the same memory area and overrides previous data)
Same question when performing a GET from DB - user A asks to see his data and user B asks to see his data (each has unique ID abviously). Will each user get his own data or a mix of the two?
To clarify - I don’t use any WIX forms or such, all FE and BE code is original and only using the wix-data API.
Thank you,
Assaf
In this scenario, you do not have to worry, it will work. I’ll try to explain:
- 2 users submitting form at the same time. As long as you do not provide an _id yourself, wix-data will provide a unique _id for both, they will be separate entries in your collection
The problem arises ONLY if you try to generate some kind of a unique id (either _id or an id in another column, like “invoice number” that should be raised sequentially [990001, 990002, etc]). In that case, there is a freak chance that (assuming last used invoice number = 990002):
a)user A uses a function that looks up last number used and raises it by 1. Number 990003 is now written to collection as last number used.
b) user B uses the same function IN BETWEEN user’s A request and the writing back to the collection of 990003. We are talking milliseconds here, but it could occur. In that case, user A retrieves 990003 and so does B. They will still get a different _id from wix-data if you do not provide one yourself, so the info will not be mixed, you will have 2 rows with different _id’s but with the same invoice number (990003). Which could be bad. There is a solution for it, but it is not your problem in this scenario.
2)same thing: they both have a different _id, so they will not be mixed.
IN short: if rows carry a different _id, you are OK on database level. But, as described in 1) you ALSO want to add another unique id (like invoice number, part number, usually sequential), that’s where problems COULD arise in a high volume scenario (but it is solvable).
Hope this helps.
What is the solution for the invoice number duplication you mentioned?
There are 2 solutions, but I will give you the most reliable one. You put a unique index on invoice number, write it, catch a possible DUPS error, if so, raise number by increment (like 1), write to db again, until no more DUPS error.
2 Likes
Did not see you for a long time, old friend!