I have a Model called distribution.plan. When the state attribute is "draft", I can add to a distribution.plan record a number of distribution.entitlement records to the plan. Then the user interface has a "confirm" button, when when that button is clicked, a method of the distribution plan model is called to create stock.moves for each entitlement.
My problem is that if 2 users click (or maybe with some browsers if 1 users double clicks) on the confirm button almost simultaneously, I will get 2 stock move generated for each entitement in the end, which is incorrect.
I can work around this with a SELECT [...] FOR UPDATE NOWAIT statement in the code of the method, but this feels wrong (and it is not an idiom I've encountered in the official addons, it's only used once in stock, and this is not the same use case). So what is the correct (framework-wise) way of dealing with this?
If the generation of the stock moves only occurs when the confirmation happens, then the change of `state` of the `distribution.plan` is your safeguard to prevent the transaction happening twice.
Provided that your confirm() method verifies the state is not already confirmed, then the database will prevent two transactions from writing state = 'confirmed' after reading state = 'draft' in parallel. Technically one of the transactions will be rolled back, replayed, and will notice that the plan is already confirmed.
Of course this is based on a number of assumptions regarding the lifecycle of your distribution plan, so your mileage may vary ;-)
Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!
About This Community
|Asked: 6/24/14, 10:39 AM|
|Seen: 1102 times|
|Last updated: 3/16/15, 8:10 AM|