Good morning community!
So, I've investigated about transactions, but there's something that is not working for me:
I have this scenario.
It's a system to registration, with limited vacancies.
For example, if I'm Male, the server action ReservaVagaMasculino is called (in other module).
Then this action is called (ReservaVagaMasculino):
And it calls DiminuirVagaMasculino, which subtracts 1 do Vaga.Masculino:
The problem is that, for example, if 5 users submit at the same time, the Vaga.Masculino is just being subtracted 1 time, instead of 5. :/
So my doubt is, when it calls the second server action (DiminuirVagaMasculino) the transaction of the first is already commited? The second server action is a new transaction? How would you handle this?
Thank youuu!
You need to pass the Identifier for a selected item on which you want to perform the Update operation to your get for update action once you do that the Id is locked to you. Now you can update it and if it is already locked by someone else you can raise an exception.For your use case you need to do some hit and try and find yourself whether it is possible that way or not.
For more step-by-step process follow this page - https://success.outsystems.com/documentation/how_to_guides/data/how_to_handle_concurrent_updates_on_application_data_records/
And this is the Sample - Handling Concurrent Update
Thanks
Tousif Khan
Hello
Instead of Update, you can use GetForUpdate in that scenario
GetForUpdate: Reads an entity record from the Database through its identifier and prevents other requests from accessing it until it's updated.This function retrieves all the attributes for the record that has the input argument Id as its Identifier. If some of the attributes are empty in the database, they are always converted to the corresponding default values.
In the database, the row that corresponds to the selected record is locked and no other user can manage its information; i.e. while the operation is being processed, no other GetForUpdate, Update, Delete or any SQL statements that change this record are executed. The lock on the record is released when a commit or rollback occurs.
And for a transaction:-
Check this documentation
Hope this helps
Great Tousif!
Only 1 doubt: can I call GetAndUpdate in one server action and just call the Update in other server action?
Thanks, I don't need to the part of the warnings, just make sure that the record is updated by everyone ;)