title: Node.js - "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR" in MySQL
body: <p>I am trying to write an Node + Express to provide REST services. Everything goes well when the application starts.</p>
  <p>However, after several hours, I got an error: &quot;code&quot;:&quot;PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR&quot;,&quot;fatal&quot;:false. It seems some resources are running out. Did someone know how to fix it.</p>
  <p>Here is the main part of code:</p>
  <pre><code>var mysql   = require(&quot;mysql&quot;);
  var _ = require('underscore')
  var Q = require(&quot;q&quot;);
  
  function REST_ROUTER(router,connection,md5) {
      var self = this;
      self.handleRoutes(router,connection,md5);
  }
  
  var queryWithPromise = function (connection, query) {
      var deferred = Q.defer();
      connection.query(query, function(err, rows, fields) {
          if (err) {
              deferred.reject(err);
          };
          if (!rows||(rows.length === 0)) {
            deferred.reject(&quot;No record!&quot;);  
          }
          deferred.resolve(rows);
      });
      return deferred.promise;    
  };
  
  
  REST_ROUTER.prototype.handleRoutes = function(router,connection,md5) {
      var self = this;
      router.get(&quot;/&quot;,function(req,res){
          res.json({&quot;Message&quot; : &quot;Hello World !&quot;});
      });
      router.get(&quot;/data/:token/:latlng&quot;,function(req,res){
          //console.log(req.params.token);
          query = mysql.format(&quot;SELECT * FROM TOKEN WHERE token=?&quot;,[req.params.token]);
          queryWithPromise(connection, query).then(function (tokens) {
              var latlngs = req.params.latlng.split(',');
              var lat = parseFloat(latlngs[0]);
              var lng = parseFloat(latlngs[1]);
              var queryLatLng = mysql.format(&quot;SELECT * FROM device WHERE longitude &gt; ? AND longitude &lt; ? AND latitude &gt; ? AND latitude &lt; ?&quot;, [lng-0.1, lng+0.1, lat-0.1, lat+0.1]);
              return queryWithPromise(connection, queryLatLng);
          }).then(function (devices) {
              var latlngs = req.params.latlng.split(',');
              var computeDistance = function (fromLatlng, toLatlng) {
                  var toRad = function (degree) {
                      return degree * Math.PI / 180;
                  };                
                  var EARTH_RADIUS = 6378137; // in meter
                  var lat1 = toRad(fromLatlng.lat);
                  var lng1 = toRad(fromLatlng.lng);
                  var lat2 = toRad(toLatlng.lat);
                  var lng2 = toRad(toLatlng.lng);
                  var d = EARTH_RADIUS*Math.acos(Math.sin(lat1)*Math.sin(lat2) + Math.cos(lat1)*Math.cos(lat2)*Math.cos(lng1 - lng2));
                  return d;
              };
              var closestDevice = _.min(devices, function(device) {
                  return computeDistance({lat: parseFloat(latlngs[0]), lng: parseFloat(latlngs[1])}, {lat: device.latitude, lng: device.longitude});
              });
              var queryDevice = mysql.format(&quot;SELECT * FROM ?? ORDER BY UploadTime DESC LIMIT 100&quot;, [closestDevice.device_id]);
              return queryWithPromise(connection, queryDevice);
          }).then(function (data) {
              var latlngs = req.params.latlng.split(',');            
              res.json({
                  &quot;latitude&quot;: parseFloat(latlngs[0]),
                  &quot;longitude&quot;: parseFloat(latlngs[1]),
                  &quot;timezone&quot;: &quot;Asia/Shanghai&quot;,
                  &quot;offset&quot;: 8,
                   &quot;hourly&quot;: {
                      &quot;data&quot;: _.map(data, function(item) {
                          return {
                              &quot;time&quot;: item.UploadTime.getTime()/1000, 
                              &quot;precipIntensity&quot;: item.rainfall,
                              &quot;temperature&quot;: item.airtemp,
                              &quot;humidity&quot;: item.airhumidity,
                              &quot;windSpeed&quot;: item.windspeed,
                              &quot;windBearing&quot;: item.winddirection,
                              &quot;pressure&quot;: item.atmosphericpressure,
                              &quot;soiltemp&quot;: item.soiltemp,
                              &quot;soilhumidity&quot;: item.soilhumidity
                          };
                      })
                   }
              });
          }).catch(function (error) {
              res.json({&quot;Error&quot; : true, &quot;Message&quot; : &quot;Success&quot;, &quot;Data&quot; : error});
          })
          .done();
      });
  }
  
  module.exports = REST_ROUTER;
  </code></pre>