Connection to SQL destroyed (after idle). Do I need to manually handle open + close for each query?
i deployed my SQL server and nodejs server on railway. The server is up and running, for some reason my SQL connection disconnected? I was wondering if I am missing something fundamental or what?
I am using this
db
in different controllers to make queries. Am i messing up somewhere?
Should I handle the open and close state manually in every query or am I missing something?6 Replies
Drizzle is not responsible for the database connection; mysql2 is. After reviewing your code, everything appears to be in order. However, the connection might be disrupted by external factors. You could consider using the Pool from mysql2, which can handle reconnections on your behalf.
so I should create a pool of connections and pass it to
drizzle(poolConnection, otherConfigs)
and then i will simply use the db
for querying right?yes, you can try this approach
didn't have any issue with it, when was using Pool
Ok i will try the pool way.
the error I am currently getting is
Can't add new command when connection is in closed state
that is maybe because my DB did not receive any request for 8 hrs or more and hence the connection was closed.I guess you won't get any of that with Pool
so if some connections are open - Pool will use it
if all of them are closed - Pool will create a new one and use it
if new request is there and connection is busy - Pool will create a new one and use it
I guess by default mysql2 has 10 connection limit in a Pool
but you can setup any size you need
this makes sense. thanks alot Andrew.