Skip to content

Instantly share code, notes, and snippets.

@Pita
Created May 1, 2011 23:39
Show Gist options
  • Save Pita/950992 to your computer and use it in GitHub Desktop.
Save Pita/950992 to your computer and use it in GitHub Desktop.
Reproduce the MySQL Driver Bug
var mysql = require("mysql");
var async = require("async");
var db;
async.waterfall([
//connect
function(callback)
{
db = new mysql.Client();
db.user = "root";
db.host = "localhost";
db.password = "";
db.database = "store";
db.connect(callback);
},
//create the table
function(result, callback)
{
var sql = "CREATE TABLE IF NOT EXISTS `store` ( " +
"`key` VARCHAR( 100 ) NOT NULL , " +
"`value` TEXT NOT NULL , " +
"PRIMARY KEY ( `key` ) " +
") ENGINE = INNODB;";
db.query(sql,[],callback);
},
//fill the table
function(result, callback)
{
var sql = "START TRANSACTION;\n";
for(var i = 0;i<10000;i++)
{
sql+= db.format("REPLACE INTO `store` VALUES (?,?);\n", ["key" + i , "value" + i ]);
}
sql += "COMMIT;";
db.query(sql,[],callback);
},
function(callback)
{
//this is a endless loop
async.whilst(
function () { return true },
function (callback)
{
async.waterfall([
//select all data
function(callback)
{
var keys = [];
for(var i = 0;i<10000;i++)
{
keys.push("key" + i);
}
//fire 10000 selects
async.forEach(keys, function(item, callback)
{
db.query("SELECT `value` FROM `store` WHERE `key` = ?", [item], callback);
},callback);
},
//replace one value in a transaction, here happens the bug, sometimes and sometimes not.
//Thats why it is in a endless loop
function(callback)
{
//we have the problem too if we use insert into instead of replace into
db.query("REPLACE INTO `store` VALUES (?,?);", ["key77", "value"],callback);
}
], callback);
}
);
}
],function(err, info){
if(err) console.error(err);
console.error("finished");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment