3

Lambda not returning the callback function if I'm not closing the connection but if I do close the connection the second time the function not working and I'm getting error: "Connecting after shutdown is not supported" The code:

client.connect(function(err) {
  console.log(err);
  const query = 'SELECT * FROM table LIMIT 1';
  client.execute(query, [], {}, function(err, result) {
    if (err) {
      console.log("there was an err");
      console.log(err);
      process.exit();
    }
    else
    {
      client.shutdown(function() {
        callback(null, {});
      });
    }
  });
});

I know that it's the way it should work by: http://docs.datastax.com/en/developer/nodejs-driver/latest/faq/#should-i-shut-down-the-pool-after-executing-a-query

But is there any solution to manage the connection on Lambda?

More info on Lambda here container reuse: https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/

jorgebg
  • 6,560
  • 1
  • 22
  • 31
Rafael Mor
  • 626
  • 1
  • 7
  • 21
  • 1
    Set `context.callbackWaitsForEmptyEventLoop = false;` inside the handler. Otherwise the callback fires but the function times out instead of returning a result. – Michael - sqlbot Sep 23 '17 at 17:07
  • Very similar to https://stackoverflow.com/q/46061612/1695906. – Michael - sqlbot Sep 23 '17 at 17:09
  • Some background on the driver: When `client.connect()` is called the first time, it will initialize the pool to each host selected by the load balancing policy (local nodes by default). The following times you call `client.connect()` it will be a no-op. In an application were you control the startup and shutdown, you should call `client.connect()` once during startup and `client.shutdown()` when shutting down the app. Outside a controlled application shutdown, you should never call `client.shutdown()`. In your case, the idea would be to reuse the `client` instance when possible. – jorgebg Sep 25 '17 at 09:57

0 Answers0