Race Conditions on a Stock

Hello everyone!

I’ve been developing an app and ran into a classic problem of software development that I do not know how to solve on bubble and thought maybe the good people of the bubble forums could help me.

The app I am developing is a tool for managing a farm, where the user create planting plans for different crops and, when they harvest a crop a trigger runs updating the stock of that crop with the amount harvested. So it queries the database to get the current stock (lets say 20 broccoli) and update it by adding the amount harvested (lets say 10 broccoli). So in the end I would have 30 broccoli on stock right?

Well, the problem of course comes if 2 harvests are entered for the same crop in a short time, as both would trigger the workflow, take the same value of initial stock (20) and add their harvested amount, resulting in one of the 2 harvests essentially not being registered.

I tried to look up online but from my understanding there isn’t a ready solution for race conditions such as DB locks on bubble, so I thought to check if anyone would have an idea of how to deal with this issue for this case I am dealing with.

If anyone has any insight, it would be greatly appreciated.

Thanks all!

I had a similar problem a while ago on a project for a client, what we ended up doing was when the data type was opened, lets say it’s a document, we trigger an action to set a flag on the document that it is being edited, we also set the user who is editing said document. This way no other users can open the document (through conditions), and if any other user just so happened to open it at the same time (very rare) we could set the save button to be conditional to the user who is authorized to be editing the document.

The main problem with this is that if the user simply closes the page without “saving” it would stay locked for other users. I forget how we approached this problem but I imagine it had to do with scheduling a backend workflow to unlock the document automatically (current date and time + 5 minutes), if the user “saves” we cancel the scheduled workflow and unlock it immediately.

There would still be a problem with the document “unlocking” while the user was still editing it, I think this can be solved by having a When condition is true workflow on the page which fires every time the Current page’s document is unlocked and it re-locks the document and schedules the workflow out again to unlock it.

Hi @justin.j! Thank you for the quick answer!

Thats a good solution, however I don’t think it applies to my case. The issue is that the change is not direct, but rather a side effect.

I could use this solution to stop anyone from harvesting crops in one specific planting plan, but then if someone harvested in another plan that has the same crops the problem would still occur.

Essentially what I think I need to do is simulate a lock system in the stock table (maybe setting up a queue?), but since this is not something I have that much experience with, specially in bubble, I wanted to see if someone have an insight or maybe a better solution.

Thank you for your reply anyway, I’ll see there’s someway of doing something analogous.

Hi there, just a little brainstorm here-

Instead of updating the total in the database, why not just create a list of inventory and sum the column of stock in that table? I’m this example, you’d have an entry with 20, and you’re creating a new entry of 10. The total calculation could just look at all entries and calculate. You could then have your field that adds these all together and stores it when new entries are created. Could an approach like this work for you?

1 Like