Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

closing connection

Status
Not open for further replies.

dvannoy

MIS
May 4, 2001
2,765
US
question....

is con.close() enough to close the connection ? or should I also add con.dispose() ?

I have been getting a message stating "Timeout expired. the timeout period elapsed prior to a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.


any info would be appreciated
 
This should not happen in newer versions of the framework since the have enhanced garbage collection. Since I don't know what version or your exact code, I suspect connections are being left open. The best thing to do is to get into the habit of using "Using" statements when opening connections to the DB. The "Using" will close the connection for you automatically once data is returned or an error occurs.
 
okay, I understand. is the below code fine or did I miss something. since I'm using "try" do I need to dispose or con ?

Using con As SqlConnection = New SqlConnection(My_Con)

Dim cmd As New SqlCommand

cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "My SP"


Try
con.Open()
cmd.ExecuteNonQuery()

Catch ex As Exception
error handling
Finally
message to user
End Try
End If

End Using


 
Better use using:
Code:
 using (var sqlConn = new SqlConnection(ConnectionString))
{
 sqlConn.Open();
....
sqlConn.Close();
}
I used Close and Dispose and have troubles, but when I started to use using() the problems gone :)

Borislav Borissov
VFP9 SP2, SQL Server
 
okay, thanks

I thought of the whole purpose of "using" was that the connection was closed automatically and you don't need to do a con.close() ? I guess it wouldn't hurt
 
With a Using statement you don't need to specifically close the connection. That is the major point of using a Using statment.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top