title: Node js (getConnection)
body: <pre><code>var nodePort = 3030;
  var express = require('express');
  var app = express();
  var bodyParser = require('body-parser');
  var db = require('mysql');
  var dbPool = db.createPool({
      host : 'localhost',
      user : 'root',
      password : '1234',
      database : 'test',
      port : 3306
  });
  
  
  app.use( bodyParser.json() );
  app.get('/api/db', function(req, res){
      res.setHeader('content-type', 'application/json');
      dbPool.getConnection(function(objErr, objConn){
          if(objErr){
              sendError(res, 503, 'error', 'connection', objErr); //503 - Service Unavailable
          }else{
              objConn.query("SELECT * FROM person", function(Err, Rows, Fields){
                  if(Err){
                      sendError(res, 500, 'error', 'query', Err);
                  }else{
                      res.send({
                          results : 'success',
                          err : '',
                          err_type : '',
                          fields : Fields,
                          rows : Rows,
                          length : Rows.length
                      });
                      objConn.release();
                  }//else
              });
          }//else
      });
  });
  /*
  app.get('/api/db:id', function(req, res){
      var id = req.params.id;
      res.setHeader('content-type', 'application/json');
      dbPool.getConnection(function(objErr, objConn){
          if(objErr){
              sendError(res, 503, 'error', 'connection', objErr); //503 - Service Unavailable
          }else{
              objConn.query("SELECT * FROM person WHERE id = ? ",[id], function(Err, Rows, Fields){
                  if(Err){
                      sendError(res, 500, 'error', 'query', Err);
                  }else{
                      res.send({
                          results : 'success',
                          err : '',
                          err_type : '',
                          fields : Fields,
                          rows : Rows,
                          length : Rows.length
                      });
                      objConn.release();
                  }//else
              });
          }//else
      });
  });
  */
  app.post('/api/db', function(req, res){
      if(!req.body.tableName){
          var data = {
              ID : req.body.id,
              Name : req.body.name
          }
          tableName = 'person';
      }else{
          var data = {
              email : req.body.email,
              regid : req.body.regid
          }
          tableName = 'users';
      }//else
      console.log(req.body.regid);
      console.log(req.body.tableName);
      console.log(req.body.email);
      res.setHeader('content-type', 'application/json');
      dbPool.getConnection(function(objErr, objConn){
          if(objErr){
              sendError(res, 503, 'error', 'connection', objErr); //503 - Service Unavailable
          }else{
              objConn.query("INSERT INTO "+tableName+" SET ? ", data, function(Err, Rows, Fields){
                  if(Err){
                      sendError(res, 500, 'error', 'query', Err);
                  }else{
                      res.send({
                          results : 'success'
                      });
                      objConn.release();
                      if(!req.body.tableName){ gcmSend(); }
                  }//else
              });
          }//else
      });
  });
  
  app.put('/api/db', function(req, res){
      var id = req.body.id;
      var data = {
          Name : req.body.name
      }
      res.setHeader('content-type', 'application/json');
      dbPool.getConnection(function(objErr, objConn){
          if(objErr){
              sendError(res, 503, 'error', 'connection', objErr); //503 - Service Unavailable
          }else{
              objConn.query("UPDATE person SET ? WHERE ID = ? ", [data,id], function(Err, Rows, Fields){
                  if(Err){
                      sendError(res, 500, 'error', 'query', Err);
                  }else{
                      res.send({
                          results : 'success'
                      });
                      objConn.release();
                      gcmSend();
                  }//else
              });
          }//else
      });
  });
  
  app.delete('/api/db/:id', function(req, res){
  
      var id = req.params.id;
      res.setHeader('content-type', 'application/json');
      dbPool.getConnection(function(objErr, objConn){
          if(objErr){
              sendError(res, 503, 'error', 'connection', objErr); //503 - Service Unavailable
          }else{
              objConn.query("DELETE FROM person WHERE ID = ? ",[id], function(Err, Rows, Fields){
                  if(Err){
                      sendError(res, 500, 'error', 'query', Err);
                  }else{
                      res.send({
                          results : 'success'
                      });
                      objConn.release();
                      gcmSend();
                  }//else
              });
          }//else
      });
  });
  
  function gcmSend(){
  
      message = new gcm.Message({
          collapseKey: 'demo',
          delayWhileIdle: true,
              timeToLive: 3,
              data: {
                  title: 'Node.js den mesaj gönderildi'
              }
      });
      sender.send(message, registrationIds, 4, function (err, result) {
          console.log(result);
      });
  }
  
  function sendError(res, iStatusCode, strResult, strType, objError){
      res.send({
          results : strResult,
          err : objError.type,
          err_type : strType
      });
  }
  
  app.listen(nodePort);
  console.log('App listening on port' + nodePort);
  </code></pre>
  
  <p>Hi, 
  <br />
  I wrote some codes to connection with nodejs mysql, i opened new connection each operation (post,get,put,delete) and release. Is this good approaching? or one connection is better? what is the difference between all operations in one connection or one connection for each operation?</p>