Sign In Register

How can we help you today?

Start a new topic
Answered

Runtime collection concurrency

Hi there,


I'm just getting started with GameSparks and I haven't been able to find much info about this.


It doesn't seem that a runtime collection can be locked explicitly, but this topic says that locks happen automatically; would it be possible to get clarification on what that means? Would the first read request on a runtime collection in a cloud code script automatically secure a lock on that collection for the entire runtime of that script?


I'm looking at a use case where I need to randomly generate a unique user-friendly string, which involves checking the record for existence before inserting. I need to be sure that the state of the collection hasn't changed between the moment I check and the moment I insert. Will that 'work' automatically?


Best Answer

It won't work automatically. What they're talking about there is for a single operation on the runtime collection.

Checking for the existence of the string, and then inserting it are 2 operations. Anything can happen in between them.


What you could do is, create a unique index on your field (https://docs.mongodb.com/manual/core/index-unique/), and then if someone has already inserted that string, your insert operation will fail as you want.




1 person has this question

Answer

It won't work automatically. What they're talking about there is for a single operation on the runtime collection.

Checking for the existence of the string, and then inserting it are 2 operations. Anything can happen in between them.


What you could do is, create a unique index on your field (https://docs.mongodb.com/manual/core/index-unique/), and then if someone has already inserted that string, your insert operation will fail as you want.




1 person likes this
Thanks, that's perfect - that field needs to be unique indexed so it fits what I need to do!
Login to post a comment