Should I open() and close() my SQL database constantly or leave it open? Should I open() and close() my SQL database constantly or leave it open? database database

Should I open() and close() my SQL database constantly or leave it open?


I tried to leave a connection open once -- I used it to stuff a repeater or something -- can't remember now.

Later in the program, I had another need to use the connection -- I think I had it so that when a user clicked a parent item in the repeater, a detail div would pop-up with more information for that item. This generated an error -- something to the effect of 'cannot open() on an open connection'.

I think the error might have been avoidable another way (like checking to see if the connection I was trying to open was already open), but as I thought about it, I realized I'd have to make that a standard practice throughout my app, and that seemed like too much work, so I just made it a standard practice to always close my connections after each use.

Connections stay in a connection pool -- I'm no whiz on that -- but if was curious about performance, I guess I'd keep that in mind, in terms of what it costs to open a connection multiple times -- whatever your situation is requiring anyway.

Another thought is that your DB admin may be able to force-close all open connections, or the db may close for some other reason. If you're not the dba, you might give a thought to risk/benefit of depending on something that you don't have long-term control over like keeping the connection open.


Three minutes is eternity on todays processors, even phone processors. I would close it and open it each time. It is better than possibly leaving handles hanging out in 'the ether'.


Let's say you have several DB operations to do back to back in a single thread. I'd keep the DB open to do those actions. However close the DB after doing a chunk of work.