✅ the best way to clean up SqlConnection
Hey guys, basically i want to clean up the sql connection after done with querying stuff on each request, i put it in using block but i notice that the connection instance (SqlConnection) still there after gone out of the using scope, it looks like Garbage Collector doesn't clean them immediately. When i check how many active connection to my database, just a small amount of them like 3 or 4, but when i check it in Rider, there was like hundred of them allocated in memory, i guess they aren't cleaned up but rather than they are being deactived and remain there waiting for the Garbage Collector to clean up, so basically i want to explicity do it myself, what is the best practice here ? Thanks for reading
3 Replies
it looks like Garbage Collector doesn't clean them immediately.Correct. GC runs only when it deems it necessary to run The solution to your problem would be architectural, I'd say Create one connection, inject it with DI, and reuse it
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.