开发者

node.js mysql socket error after stress test

开发者 https://www.devze.com 2023-04-04 01:23 出处:网络
I use node.js, an asynchronous event base server with mysql module to select 122 rows of records from the database.

I use node.js, an asynchronous event base server with mysql module to select 122 rows of records from the database.

I ran a simple stress test using the command below:

ab -n 10 -c 2 http://localhost:8000/

the node.js finish the process, but mysql is still running, until it hits an error:

 throw new Error('Socket is not writable');
          ^
 Error: Socket is not writable
    at Socket._writeOut (net.js:391:11)
    at Socket.write (net.js:377:17)
    at Client.write (/home/kelvin/node_modules/mysql/lib/client.js:142:16)
    at Object.end [as fn] (/home/kelvin/node_modules/mysql/lib/client.js:240:10)
    at Client._dequeue (/home/kelvin/node_modules/mysql/lib/client.js:274:18)
    at Object.end [as fn] (/home/kelvin/node_modules/mysql/lib/client.js:247:10)
    at Client._dequeue (/home/kelvin/node_modules/mysql/lib/client.js:274:18)
    at Object.end [as fn] (/home/kelvin/node_modules/mysql/lib/client.js:247:10)
    at开发者_Go百科 Client._dequeue (/home/kelvin/node_modules/mysql/lib/client.js:274:18)
    at Object.end [as fn] (/home/kelvin/node_modules/mysql/lib/client.js:247:10)

Now whenever I run the test again, I get the same error.

Is MySQL a good solution to use with Node.js or is it how I code?

Some node.js code:

var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});

  client.query('USE ' + db);
  client.query('SELECT * FROM ' + tbl,
      function selectDb(err, results, fields) {
        if (err) {
          throw err;
        }

        console.log(results);
        client.end();
      }
  );

  response.end("END RESULT");
});


You share one mysql connection object for every connection: When two clients request a page simultanously you maybe destroy the internal state of your mysql library while it is not intended to send a new query when another query is right now executing.

Use one mysql connection for every request, you can use node-pool if you want pooling for mysql connections.

0

精彩评论

暂无评论...
验证码 换一张
取 消