Forum Moderators: open
I was asked to do some research on weather...
I have an windows application which connects to the a SQL db, code being used is .net, SHOULD this connection be opened once(to the SQL DB)and run as long as the app. is running or should it be opened and closed as the app. needs it.
WHY?
I would personally open and close it as the app. needs it.
I would just like to know what is been done out there.
Thanks
It would depend a lot on how often and how the connection would be used. How much time normally would there be between needing the connection?
One possible middle ground would be to build in a single connection pool, where an open connection is kept open for 5 minutes, and then closed if it has not been reused.
It also depends on how "polished" the application is expected to be.
The overhead with connection is usually only experienced the first time a connection is made. If connections are frequent, the connections after the first will actually be grabbed from the pool on the local machine (these happen rather quickly). If conenctions aren't frequent, chances are the connection object is still in memory and will load quickly.
Another reason to open and close the connection is that at the opening you can fail gracefully if the connection doesn't happen. When you leave a connection open, the status of the connection can change without the connection object knowing. Meaning that if you have an open connection and the network goes down or server resets, the connection object still thinks that it's open and active. You just get a funky error when you go to execute the sql.
-=casey=-