title: How to automatically close mysql connection in nodejs
body: <p>Currently my process is hanging because the mysql pool isn't being closed automatically.</p>
  
  <p>I assumed that when I called <code>connection.release()</code> the connection would get closed if no one else was using it. But it seems that is not the case. My code looks like:</p>
  
  <p>getConnection.js:</p>
  
  <pre><code>var mysql = require('mysql');
  
  var pool = mysql.createPool({
    connectionLimit : 10,
    host     : 'localhost',
    user     : 'root',
    password : 'mysql',
    database : 'xxx'
  });
  
  module.exports = function getConnection(callback){
      pool.getConnection(function(err, connection){
          if (err) {
              throw err;
          }
          callback(connection);
      });
  }
  </code></pre>
  
  <p>And i'm using it like this:</p>
  
  <pre><code>var getConn = require('./getConnection');
  
  function selectOne(query, map, cb){
  
      getConn(function(conn){
          conn.query(query, map, function(err, rows){
              if (err) {
                  throw err;
              }
  
              cb(rows.length ? rows[0] : false);
              conn.release();
          });
      });
  }
  </code></pre>
  
  <p>I think I'm missing something. Shouldn't node-mysql close the connection for me when I release it?</p>