Mongo DB eval nolock:true

1.3k views Asked by At

Hi,
I am running the eval script from node js making the nolock true to disable the global lock.The same eval script i am running from the java also with nolock true.

String jsFunction = "function(){"
                          + "var uid = 12;"
                            + "return refreshList(uid,false);}";
DBObject commandObj = new BasicDBObject();
commandObj.put("eval", jsFunction);
commandObj.put("nolock", true);
CommandResult status =db.command(commandObj);
db.eval("function (x, isoverwrite) { return refreshList(x, isoverwrite);}",                                                              [21,isoverwrite] ,{nolock:true}, function(err, result){
    });

In java while running the script there was no lock in db and i can able to run the query simultaneously.But in node js running the above code i am no able to do other process simulatneously.I am not able to see lock logs in mongodb console also. i don't know why the behaviour changes in both. I also tried using command query in node js but i got the same issue.

1

There are 1 answers

4
Adam Comerford On

By default, the eval function itself will take and hold a global write lock. By setting nolock to true all you are doing is preventing that lock from being taken by eval. Setting nolock does not impact whether operations within the JavaScript code itself takes a write lock, so any update, insert, findAndModify etc. operation that you perform in the code itself will still take a lock just like any other write operation (for reference, this is essentially straight from the docs).

There is no alternative or way around this, you can't write data to the database without impacting your locking, if you are writing data (including updates) then you have to take a write lock. Running operations server side and inside an eval statement does not change that.

It's worth noting that the warning message you have posted is just that, a warning. It means that the query in question tried to yield (usually because it was doing a fault to disk) and could not because of the nature of the lock it held at the time - it is not an error. I would recommend running this outside an eval block to see what it looks like then and remove eval from the equation entirely.