serverless-mysql icon indicating copy to clipboard operation
serverless-mysql copied to clipboard

Thread doesn't get cleaned up?

Open sjns19 opened this issue 3 years ago • 4 comments

Hi, I've recently started using serverless mysql specially because of my Next.js environment and I was checking around for proper connection management and while logging the event actions, I figured the connection thread ID keeps on increasing every time I refresh my API URL on the browser.

I have the connection created, data fetched then closed the connection with connection.quit() as connection.end() didn't work, as shown below

Screen Shot 2022-08-21 at 4 06 53 PM

And the console shows the thread ID like

Screen Shot 2022-08-21 at 4 02 00 PM

I tried restarting the server but the thread ID still keeps on increasing.

I don't know if this is an okay behavior but from my knowledge using MySQL with different languages previously, there should only ever be a single thread created and removed as soon as the connection is closed if there aren't multiples of operations to be performed, right? Should I be worried or this is normal?

sjns19 avatar Aug 21 '22 10:08 sjns19

The library was designed for use with serverless functions that have a single concurrency model, so you should avoid using .quit() on every call in order to freeze and reuse the connection on subsequent invocations wherever possible.

.end() is a clean up method that only terminates connections when necessary. As far as the thread number is concerned, I’m guessing that’s likely an internal node thing.

jeremydaly avatar Aug 21 '22 11:08 jeremydaly

The library was designed for use with serverless functions that have a single concurrency model, so you should avoid using .quit() on every call in order to freeze and reuse the connection on subsequent invocations wherever possible.

.end() is a clean up method that only terminates connections when necessary. As far as the thread number is concerned, I’m guessing that’s likely an internal node thing.

Thanks for the response. So it's completely safe and fine to keep the connection alive? I figured even without closing the connection, the thread ID keeps on increasing on each request made to the API. Would there be any memory leak issue or something like this caused?

sjns19 avatar Aug 21 '22 11:08 sjns19

It’s preferable to keep the connection alive as it saves time when you don’t set up and tear connections down. Memory should be a problem.

jeremydaly avatar Aug 21 '22 13:08 jeremydaly

It’s preferable to keep the connection alive as it saves time when you don’t set up and tear connections down. Memory should be a problem.

Ok thank you very much. I figured that the thread id increasing is because of the hot reload feature of Next.js. I just felt dumb lol

And sorry for creating an issue for something like this. You can close it.

sjns19 avatar Aug 22 '22 04:08 sjns19