I'm using node-mysql in my application. I tried implementing SELECT FOR UPDATE but I haven't been able to get it to work properly. The problem I have looks like the code below. The first transaction in the for loop below does not successfully block the second transaction. Instead, both transactions SELECT FOR UPDATE at the same time and get the same record. How can I fix this? Thanks!
var testTransaction = function (count) {
connection.beginTransaction(function(err) {
if (err) throw err;
db.query('SELECT * FROM myTable WHERE id = 1 FOR UPDATE', function(err, rows, result) {
if (err) {
connection.rollback(function() {
throw err;
});
}
connection.query('UPDATE myTable SET myField=? WHERE id=1', (count + 1), function(err, result) {
if (err) {
db.rollback(function() {
throw err;
});
}
connection.commit(function(err) {
if (err) {
db.rollback(function() {
throw err;
});
}
console.log('success!');
});
});
});
});
}
for (var i = 0; i < 2; i++) {
testTransaction(i);
}