I'm working in Sails.js (v0.12.13), and one of my actions in the controller looks like:
create: function(req, res){
var comment = req.body;
var image_id = req.params.id;
Image.findOne(image_id).populate('comments').exec(function(err, image){
image.messages.add(comment);
image.save(function(err){
return res.created(comment);
});
});
}
(Error handling was ommited)
Basically this adds a comment to an image. First, I need to get the image, with it's comments, add the new comment to the array, and save it again.
However, my intuition is that there's a case when two different people try to add a comment, and the order of events is:
Can this happen in Node.js?, or is there something that prevents this from happening?
I've seen some examples in the website, like http://sailsjs.com/documentation/reference/waterline-orm/populated-values/add where they do something similar. If race condition can happen, then Sail.js becomes unuseable for me, because I find these things very important.
Thanks in advance.
Short answer: Don't worry, you'll not lose / overwrite data this way.
Can this happen in Node.js?
Yes
is there something that prevents this from happening?
Yes
This condition is not specific to Node.js or asynchronous, event loop based execution.
Similar thing can happen with 2 threads handling 2 requests in other languages (Java, Ruby etc.) due to thread preemption.
{ id: 1, comments: [1, 2] }{ id: 1, comments: [1, 2] }{ id: 1, comments: [1, 2, 3] }. On save, it ensures that only 1, 2, 3 comments are associated with Image 1.{ id: 1, comments: [1, 2, 4] }. On save, it ensures that only 1, 2, 4 comments are associated with Image 1, thereby removing comment 3Comment#3 to be added. Something like { id: 1, comments: { value: [1, 2], addModels: [3] }. On save, an association is created between Comment#3 and Image#1 in database.Comment#4 to be added. Something like { id: 1, comments: { value: [1, 2], addModels: [4] }. On save, an association is created between Comment#4 and Image#1 in database. Earlier created association is Comment#3 is not touched.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With