# How to properly close the database connection in a lambda function?

**URL:** https://forum.serverless.com/t/how-to-properly-close-the-database-connection-in-a-lambda-function/2545
**Category:** Serverless Framework
**Tags:** aws
**Created:** [August 22, 2017, 4:02am UTC](https://forum.serverless.com/t/how-to-properly-close-the-database-connection-in-a-lambda-function/2545 "2017-08-22T04:02:58Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![THPubs](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/thpubs/32/891_2.png) [@THPubs](https://forum.serverless.com/u/THPubs)
#### Post date: [August 22, 2017, 4:02am UTC](https://forum.serverless.com/t/how-to-properly-close-the-database-connection-in-a-lambda-function/2545/1 "2017-08-22T04:02:58Z")

</div>

In my lambda function, I tried to close the mongo connection as soon as I send a callback. But it has a problem.

- When I send a request, the function performs its duties, send the callback and close the database connection.
- When the second request is sent, the function times out.
- When I remove the `db.close()` things work perfectly.

I think lambda re-use the connection for all the functions because I open the connection in the top of the handler:

```
// Connect to database
mongoose.connect(process.env.DATABASE_URL);

const handleCreateUser = async (event, context, callback) => {
  // eslint-disable-next-line no-param-reassign
  context.callbackWaitsForEmptyEventLoop = false;

  const data = JSON.parse(event.body);
  const { user, userProfile } = data;

  await createUser({ callback, user, userProfile });
};

```

Any idea what how to fix this? Do we really have to close the connection at this point?

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [August 22, 2017, 4:54am UTC](https://forum.serverless.com/t/how-to-properly-close-the-database-connection-in-a-lambda-function/2545/2 "2017-08-22T04:54:36Z")

</div>

Code outside the handler is only executed when the Lambda is initialized during a cold start. When the Lambda runs a second time it’s trying to use a connection that’s been closed. Either move the `mongoose.connect()` inside the `handleCreateUser` or look at connection pooling.

---

<div class="post-metadata">

### Author: ![THPubs](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/thpubs/32/891_2.png) [@THPubs](https://forum.serverless.com/u/THPubs)
#### Post date: [August 22, 2017, 5:30am UTC](https://forum.serverless.com/t/how-to-properly-close-the-database-connection-in-a-lambda-function/2545/3 "2017-08-22T05:30:36Z")

</div>

Is there a way to close the connection just before the lambda function exit?

---

<div class="post-metadata">

### Author: ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)
#### Post date: [August 22, 2017, 11:28pm UTC](https://forum.serverless.com/t/how-to-properly-close-the-database-connection-in-a-lambda-function/2545/4 "2017-08-22T23:28:08Z")

</div>

Yes. Just close it at the bottom of the handler function but remember to move the open to the top of the handler function or it won’t run a second time

---

<div class="post-metadata">

### Author: ![rowanu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rowanu/32/75_2.png) [@rowanu](https://forum.serverless.com/u/rowanu)
#### Post date: [August 24, 2017, 9:25pm UTC](https://forum.serverless.com/t/how-to-properly-close-the-database-connection-in-a-lambda-function/2545/5 "2017-08-24T21:25:45Z")

</div>

Just to add to @buggy’s answer, I attempted to visualise what he’s described in [a blog post](http://blog.rowanudell.com/database-connections-in-lambda/).

---

<div class="post-metadata">

### Author: ![THPubs](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/thpubs/32/891_2.png) [@THPubs](https://forum.serverless.com/u/THPubs)
#### Post date: [August 26, 2017, 3:34am UTC](https://forum.serverless.com/t/how-to-properly-close-the-database-connection-in-a-lambda-function/2545/6 "2017-08-26T03:34:24Z")

</div>

Thanks @buggy I think that’s the best way to do it. Thank you @rowanu your article explained a lot more! Now things are getting more clear 🙂 😃

---

<div class="post-metadata">

### Author: ![kaiyes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/kaiyes/32/1278_2.png) [@kaiyes](https://forum.serverless.com/u/kaiyes)
#### Post date: [November 16, 2017, 4:06am UTC](https://forum.serverless.com/t/how-to-properly-close-the-database-connection-in-a-lambda-function/2545/7 "2017-11-16T04:06:09Z")

</div>

offtopic. @THPubs nice to see you here too. Saw you in meteor forums !

---

<div class="post-metadata">

### Author: ![THPubs](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/thpubs/32/891_2.png) [@THPubs](https://forum.serverless.com/u/THPubs)
#### Post date: [September 16, 2018, 6:24am UTC](https://forum.serverless.com/t/how-to-properly-close-the-database-connection-in-a-lambda-function/2545/8 "2018-09-16T06:24:58Z")

</div>

Guys… Almost an year have passed. In the present day, is there a better solution for the mongo database connection closing issue?
