Skip to main content
spelling cleanup
Source Link
gnat
  • 20.5k
  • 29
  • 117
  • 310

onOn nodejs with node-mysql, should iI create 1one connection for the service like this:

or 1or for each user like this:

what i'mWhat I'm asking is:

  • is the first one faster because the connection is always open and on the second one it needs to create every time a user connects?
  • is the first one slower because if many users are asking for queries he only does one at the time, and on the second one each one have his own client and can make multiple connections at the same time?
  • is the second one heavier for the node server and for the mysql because it needs to have 1 client for each user?
  • is the first one heavier because it never closes the mysql connection, and after some queries may have some memory leaks?
  • or should iI create and close a mysql connection for every query?

psPS: the application connected to the node server will have a big number of users,connected connected during a long period of time, but will have a very low number of queries, but the users probably will ask for queries at about the same time.

on nodejs with node-mysql, should i create 1 connection for the service like this:

or 1 for each user like this:

what i'm asking is:

  • is the first one faster because the connection is always open and on the second one it needs to create every time a user connects?
  • is the first one slower because if many users are asking for queries he only does one at the time, and on the second one each one have his own client and can make multiple connections at the same time?
  • is the second one heavier for the node server and for the mysql because it needs to have 1 client for each user?
  • is the first one heavier because it never closes the mysql connection, and after some queries may have some memory leaks?
  • or should i create and close a mysql connection for every query?

ps: the application connected to the node server will have a big number of users,connected during a long period of time, but will have a very low number of queries, but the users probably will ask for queries at about the same time

On nodejs with node-mysql, should I create one connection for the service like this:

or or for each user like this:

What I'm asking is:

  • is the first one faster because the connection is always open and on the second one it needs to create every time a user connects?
  • is the first one slower because if many users are asking for queries he only does one at the time, and on the second one each one have his own client and can make multiple connections at the same time?
  • is the second one heavier for the node server and for the mysql because it needs to have 1 client for each user?
  • is the first one heavier because it never closes the mysql connection, and after some queries may have some memory leaks?
  • or should I create and close a mysql connection for every query?

PS: the application connected to the node server will have a big number of users, connected during a long period of time, but will have a very low number of queries, but the users probably will ask for queries at about the same time.

added 152 characters in body
Source Link
Joaolvcm
  • 183
  • 1
  • 5
var client = mysql.createClient({
    user: 'user',
    password: 'pass' 
}); // Create a new mysql Client
 
 io.sockets.on("connection", function (user)) // when a user enters
 {
    result = client.query('check user'); //check for user on DB
    if (result)
  {
        user.on('do stuff', function (data) // set to do something on ask
        {
            client.query('some stuff'); //run the mysql client to work
        });
    } else
  {
        user.disconnect();
    }
 });
}
io.sockets.on("connection", function (user)) // when a user enters
{
    var client = mysql.createClient({
        user: 'user',
        password: 'pass'
    }); // Create a new mysql Client when the users enters
    result = client.query('check user'); //check for user on DB
    if (result) 
 {
        user.on('do stuff', function (data) // set to do something on ask
        {
            client.query('some stuff'); //run the mysql client to work
        });
        user.on('disconnect', function (data) {
   {
         client.close();
        });
    } else
  {
        user.disconnect();
        client.close();
    }
}
var client = mysql.createClient({user:'user',password:'pass'}); // Create a new mysql Client
 
 io.sockets.on("connection",function(user)) // when a user enters
 {
  result = client.query('check user'); //check for user on DB
  if(result)
  {
   user.on('do stuff',function(data) // set to do something on ask
   {
    client.query('some stuff'); //run the mysql client to work
   });
 }else
 {
 user.disconnect();
}
 });
}
io.sockets.on("connection",function(user)) // when a user enters
{
  var client = mysql.createClient({user:'user',password:'pass'}); // Create a new mysql Client when the users enters
 result = client.query('check user'); //check for user on DB
 if(result) 
 {
   user.on('do stuff',function(data) // set to do something on ask
   {
    client.query('some stuff'); //run the mysql client to work
   });
   user.on('disconnect',function(data)
   {
    client.close();
   });
 }else
  {
  user.disconnect();
  client.close();
 }
}
var client = mysql.createClient({
    user: 'user',
    password: 'pass' 
}); // Create a new mysql Client
io.sockets.on("connection", function (user)) // when a user enters
{
    result = client.query('check user'); //check for user on DB
    if (result) {
        user.on('do stuff', function (data) // set to do something on ask
        {
            client.query('some stuff'); //run the mysql client to work
        });
    } else {
        user.disconnect();
    }
});
}
io.sockets.on("connection", function (user)) // when a user enters
{
    var client = mysql.createClient({
        user: 'user',
        password: 'pass'
    }); // Create a new mysql Client when the users enters
    result = client.query('check user'); //check for user on DB
    if (result) {
        user.on('do stuff', function (data) // set to do something on ask
        {
            client.query('some stuff'); //run the mysql client to work
        });
        user.on('disconnect', function (data) {
            client.close();
        });
    } else {
        user.disconnect();
        client.close();
    }
}
Source Link
Joaolvcm
  • 183
  • 1
  • 5

mysql, one connection vs multiple

on nodejs with node-mysql, should i create 1 connection for the service like this:

var client = mysql.createClient({user:'user',password:'pass'}); // Create a new mysql Client

 io.sockets.on("connection",function(user)) // when a user enters
 {
  result = client.query('check user'); //check for user on DB
  if(result)
  {
   user.on('do stuff',function(data) // set to do something on ask
   {
    client.query('some stuff'); //run the mysql client to work
   });
 }else
{
 user.disconnect();
}
 });
}

or 1 for each user like this:

io.sockets.on("connection",function(user)) // when a user enters
{
  var client = mysql.createClient({user:'user',password:'pass'}); // Create a new mysql Client when the users enters
 result = client.query('check user'); //check for user on DB
 if(result) 
 {
   user.on('do stuff',function(data) // set to do something on ask
   {
    client.query('some stuff'); //run the mysql client to work
   });
   user.on('disconnect',function(data)
   {
    client.close();
   });
 }else
 {
  user.disconnect();
  client.close();
 }
}

what i'm asking is:

  • is the first one faster because the connection is always open and on the second one it needs to create every time a user connects?
  • is the first one slower because if many users are asking for queries he only does one at the time, and on the second one each one have his own client and can make multiple connections at the same time?
  • is the second one heavier for the node server and for the mysql because it needs to have 1 client for each user?
  • is the first one heavier because it never closes the mysql connection, and after some queries may have some memory leaks?
  • or should i create and close a mysql connection for every query?

ps: the application connected to the node server will have a big number of users,connected during a long period of time, but will have a very low number of queries, but the users probably will ask for queries at about the same time