Skip to content
Advertisement

loosing connection to sql server from express js api

Im building this api that gets data from a database and posts data to another database both located on the same server but it seems that im having problems with the connections when i first run the app everything works fine but after making some api calls it just crashes and doesnt work again my guess is connections are not being closed

this is my config file theres another file just like that but with a different database

these codes prepare the connection pools for the 2 databases

and here is how i get the data

and thats how im making my api call

Advertisement

Answer

There are three issues in your codes.

First, don’t need to catch in DB connection and instead catch in the getInvoiceDetails because you may end up returning a err instead DB instance in your connection function.

Second, don’t pass variable directly in SQL because of SQL injection. You should use input() like below to guard the type of variables. You may check against the datatype of specific field for your case.

Third, the DB instance in the finally block actually have no reference to the DB instance of the try block because they are in different block. So, you can do like the code I suggest in the Second issue above.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement