views:

78

answers:

2

I am having a hard time figuring out why the following constructor will not close connnections. When I view the active connections. Here is the code that I have.

Public Sub New(ByVal UserID As Integer)

Dim oConn As New SqlConnection(ConfigurationManager.ConnectionStrings("connStr").ToString())
Dim cmd As New SqlCommand("stored proc", oConn)
Dim sdr As SqlDataReader

cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@userID", UserID)
oConn.Open()
sdr = cmd.ExecuteReader()

Try
    If Not sdr.HasRows Then
        sdr.Close()

        If Not oConn Is Nothing Then
            If oConn.State <> ConnectionState.Closed Then
                oConn.Close()
            End If
        End If
        cmd.Dispose()

        Exit Sub
    End If
    'User has account in WATS, proceed to load account information
    While sdr.Read
         _firstname = Convert.ToString(sdr("First Name"))
        _lastname = Convert.ToString(sdr("Last Name"))
    End While

Catch ex As Exception
    'Throw New Exception("User Error: " + ex.Message)
Finally
    sdr.Close()

    If Not oConn Is Nothing Then
        If oConn.State <> ConnectionState.Closed Then
            oConn.Close()
        End If
    End If
    cmd.Dispose()
End Try

End Sub
+2  A: 

Probably open due to connection pooling.

http://stackoverflow.com/questions/9228/ado-net-connection-pooling-sqlserver

Raj Kaimal
It turns out it is connection pooling. All of the connections are in a sleep state. I need to take a look as to why this sproc is being called so many times. But I thought that if I am passing the same parameters to the db then it would use the pooled connection. Is there something I am misunderstanding or doing wrong?
Are you using the same connection string?
Raj Kaimal
Yes, all calls are to the same db using the same connection.
The error I am getting is "Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached."So it seems that the problem may not be that I am forgetting to close connections but that I am opening too many. Does that sound right?
No that does not sound right. Could you remove the exception comment. See if there are any exceptions.
Raj Kaimal
I have removed it. The problem is that there are too many connections going on. I am actually going through all calls to make sure an exception is handled correctly. I also increased the max pool size and it seems to be working fine
+2  A: 

You need to be implementing Using blocks. Partial fix:

Using oConn As New SqlConnection(ConfigurationManager.ConnectionStrings("connStr").ToString()) 
    Using cmd As New SqlCommand("stored proc", oConn) 

        cmd.CommandType = CommandType.StoredProcedure 
        cmd.Parameters.AddWithValue("@userID", UserID) 
        oConn.Open() 
        Using sdr As SqlDataReader = cmd.ExecuteReader() 
        End Using
    End Using
End Using
John Saunders
I would question the word "need" because this is one alternative to a route already being partially taken in the question with Try Finally.
John K
@jdk: the answer is that it's much easier to get `Using` blocks right than try/finally.
John Saunders
@John S. I agree with Using being easier.
John K