Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why disconnect from a database?

Background info: I'm coding with C#, using Microsoft SQL Server for databases.

I didn't find much on Google on the subject, so I'm asking here: should I always close a connection to my database after performing a query?

I'm torn between two solutions (maybe better ones exist...):

  • either open the connection before querying, then close it right after the SQL query

  • or open the connection at the start of my application, and before each SQL query check if the connection is still up and reopen it if needed.

In the past, I used the first solution but I discovered that opening a new connection can take quite some time (especially over a VPN connection to my LAN opened through 3G), and that it would slow down my application. That's why I decided to go with the second solution (in that case, my connection should be always up if we forget about time-out) and noticed some better performances.

Do I need to close the connection at the end of my application or can I forget about it?

like image 538
Otiel Avatar asked Jul 01 '11 14:07

Otiel


People also ask

Why should we close database connection?

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.

How do I disconnect from a database?

To disconnect from a database, click the connection in the Database Navigator or Projects view, and then click the Disconnect button in the toolbar or click Database -> Disconnect on the main menu: You can also right-click the connection and click Disconnect on the context menu.

Why is database connection important?

A database connection is a facility in computer science that allows client software to talk to database server software, whether on the same machine or not. A connection is required to send commands and receive answers, usually in the form of a result set. Connections are a key concept in data-centric programming.

Should I close DB connection after query?

For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.


2 Answers

Yes, you should close your connection after each SQL query. The database connection pool will handle the physical network connection, and keep it open for you. You say that you found that opening a connection can take some time - did you find that the application was really doing that multiple times?

(I hope your real application won't be talking directly to the database over 3G, btw... presumably this is just for development purposes...)

like image 119
Jon Skeet Avatar answered Sep 30 '22 19:09

Jon Skeet


One important thing to remember is that there is a unique connection pool for each unique connection string you use... so always use the same connection string unless you need to connect to a different database (or have unique requirements).

Here is a good document on connection pooling with System.Data.SqlClient.SqlConnection.

like image 43
agent-j Avatar answered Oct 03 '22 19:10

agent-j