Should I keep a database connection open?
- Should I keep DB connection open or close?
- Should I close connection to database?
- What happens if you don't close a database connection?
- When should I open a database connection?
Should I keep DB connection open or close?
For fast response time and high throughput, it's actually best to keep database connections open and reuse them for subsequent requests. Most database frameworks offer some kind of connection pool mechanism where a request handler can get a database connection for its work and return it to the pool afterwards.
Should I close connection to database?
If you do not close your database connections, many problems can occur like web pages hanging, slow page loads, and more. Think of it as going through a door to your house. Maybe the door will shut by itself, but maybe it won't. If it doesn't shut, who knows what will happen.
What happens if you don't close a database connection?
If we don't close the connection, it will lead to connection memory leakage. Until application server/web server is shut down, connection will remain active, even if the user logs out. There are additional reasons. Suppose database server has 10 connections available and 10 clients request for the connection.
When should I open a database connection?
Usually you'd only want to open a connection to your database when you need to use that connection. Keeping connections open can increase the chance that part of your code will accidentally, or maliciously through the actions of others, cause unwanted queries to be performed on the database.
Related Questions
-
Anonymous2 weeks ago
Expert answer2 weeks ago -
Anonymous2 weeks ago
Expert answer2 weeks ago -
Anonymous2 weeks ago
Expert answer2 weeks ago -
Anonymous2 weeks ago
Expert answer2 weeks ago -
Anonymous2 weeks ago
Expert answer2 weeks ago -
Anonymous2 weeks ago
Expert answer2 weeks ago -
Anonymous2 weeks ago
Expert answer2 weeks ago