Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Closing connections

Reply
Thread Tools

Closing connections

 
 
David C
Guest
Posts: n/a
 
      10-11-2009
I have some class functions that return SqlDataReader to fill DropDownLists.
Below is example. I have the RETURN line before the closing of reader and
connection. Are these getting closed or is there another way to do this?
For example, if I had it wrapped in a Using block? I am getting connection
pool timeouts and am only testing the site so I'm not sure why. Thanks in
advance.

David

Public Shared Function GetWICounties() As SqlDataReader
Dim conData As SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrin gs("MCFICoreConnectionString").ConnectionString)
conData.Open()

Dim strSQL As String

strSQL = "SELECT CountyID, County" & _
" FROM dbo.WICounties" & _
" ORDER BY County"

Dim cmdSel As SqlCommand = New SqlCommand(strSQL, conData)
Dim dtr As SqlDataReader = cmdSel.ExecuteReader()
Return dtr
dtr.Close()
conData.Close()
End Function


 
Reply With Quote
 
 
 
 
Mr. Arnold
Guest
Posts: n/a
 
      10-11-2009

"David C" <> wrote in message
news:...
>I have some class functions that return SqlDataReader to fill
>DropDownLists. Below is example. I have the RETURN line before the closing
>of reader and connection. Are these getting closed or is there another way
>to do this? For example, if I had it wrapped in a Using block? I am getting
>connection pool timeouts and am only testing the site so I'm not sure why.
>Thanks in advance.
>
> David
>
> Public Shared Function GetWICounties() As SqlDataReader
> Dim conData As SqlConnection = New
> SqlConnection(ConfigurationManager.ConnectionStrin gs("MCFICoreConnectionString").ConnectionString)
> conData.Open()
>
> Dim strSQL As String
>
> strSQL = "SELECT CountyID, County" & _
> " FROM dbo.WICounties" & _
> " ORDER BY County"
>
> Dim cmdSel As SqlCommand = New SqlCommand(strSQL, conData)
> Dim dtr As SqlDataReader = cmdSel.ExecuteReader()
> Return dtr
> dtr.Close()
> conData.Close()
> End Function



You don't return a datareader, and it looks like you left everything open
without closing them before you left.

You read the data with a datareader that is reading the data from the
resultset at the spot of the returned resultset, you instantiate a DTO (Data
Transfer Object of the required properties), you add the DTO
to List<T> and you return the List<T> and bind the List<T> (which is
bindable) to the control.

The return of the List<T> will be the last statement in the function with
reader and connection closed before you get to the return.

http://www.codeguru.com/vb/gen/vb_mi...cle.php/c7063/






__________ Information from ESET NOD32 Antivirus, version of virus signature database 4498 (20091011) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



 
Reply With Quote
 
 
 
 
David C
Guest
Posts: n/a
 
      10-12-2009

"Mr. Arnold" <MR. > wrote in message
news:...
>
> "David C" <> wrote in message
> news:...
>>I have some class functions that return SqlDataReader to fill
>>DropDownLists. Below is example. I have the RETURN line before the closing
>>of reader and connection. Are these getting closed or is there another
>>way to do this? For example, if I had it wrapped in a Using block? I am
>>getting connection pool timeouts and am only testing the site so I'm not
>>sure why. Thanks in advance.
>>
>> David
>>
>> Public Shared Function GetWICounties() As SqlDataReader
>> Dim conData As SqlConnection = New
>> SqlConnection(ConfigurationManager.ConnectionStrin gs("MCFICoreConnectionString").ConnectionString)
>> conData.Open()
>>
>> Dim strSQL As String
>>
>> strSQL = "SELECT CountyID, County" & _
>> " FROM dbo.WICounties" & _
>> " ORDER BY County"
>>
>> Dim cmdSel As SqlCommand = New SqlCommand(strSQL, conData)
>> Dim dtr As SqlDataReader = cmdSel.ExecuteReader()
>> Return dtr
>> dtr.Close()
>> conData.Close()
>> End Function

>
>
> You don't return a datareader, and it looks like you left everything open
> without closing them before you left.
>
> You read the data with a datareader that is reading the data from the
> resultset at the spot of the returned resultset, you instantiate a DTO
> (Data Transfer Object of the required properties), you add the DTO
> to List<T> and you return the List<T> and bind the List<T> (which is
> bindable) to the control.
>
> The return of the List<T> will be the last statement in the function with
> reader and connection closed before you get to the return.
>
> http://www.codeguru.com/vb/gen/vb_mi...cle.php/c7063/
>
>
>
>
>
>
> __________ Information from ESET NOD32 Antivirus, version of virus
> signature database 4498 (20091011) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
>


So if I change the one line to below will this be ok? Thanks

Dim dtr As SqlDataReader =
cmdSel.ExecuteReader(Data.CommandBehavior.CloseCon nection)


 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      10-12-2009
unless there is an error, or your code that uses the reader does not
read all rows and resultsets.

returning a datareader is a bad practice (should return an object
collection in most cases) . if you do the caller shoudl use a using
statment.

-- bruce (sqlwork.com)


David C wrote:
> "Mr. Arnold" <MR. > wrote in message
> news:...
>> "David C" <> wrote in message
>> news:...
>>> I have some class functions that return SqlDataReader to fill
>>> DropDownLists. Below is example. I have the RETURN line before the closing
>>> of reader and connection. Are these getting closed or is there another
>>> way to do this? For example, if I had it wrapped in a Using block? I am
>>> getting connection pool timeouts and am only testing the site so I'm not
>>> sure why. Thanks in advance.
>>>
>>> David
>>>
>>> Public Shared Function GetWICounties() As SqlDataReader
>>> Dim conData As SqlConnection = New
>>> SqlConnection(ConfigurationManager.ConnectionStrin gs("MCFICoreConnectionString").ConnectionString)
>>> conData.Open()
>>>
>>> Dim strSQL As String
>>>
>>> strSQL = "SELECT CountyID, County" & _
>>> " FROM dbo.WICounties" & _
>>> " ORDER BY County"
>>>
>>> Dim cmdSel As SqlCommand = New SqlCommand(strSQL, conData)
>>> Dim dtr As SqlDataReader = cmdSel.ExecuteReader()
>>> Return dtr
>>> dtr.Close()
>>> conData.Close()
>>> End Function

>>
>> You don't return a datareader, and it looks like you left everything open
>> without closing them before you left.
>>
>> You read the data with a datareader that is reading the data from the
>> resultset at the spot of the returned resultset, you instantiate a DTO
>> (Data Transfer Object of the required properties), you add the DTO
>> to List<T> and you return the List<T> and bind the List<T> (which is
>> bindable) to the control.
>>
>> The return of the List<T> will be the last statement in the function with
>> reader and connection closed before you get to the return.
>>
>> http://www.codeguru.com/vb/gen/vb_mi...cle.php/c7063/
>>
>>
>>
>>
>>
>>
>> __________ Information from ESET NOD32 Antivirus, version of virus
>> signature database 4498 (20091011) __________
>>
>> The message was checked by ESET NOD32 Antivirus.
>>
>> http://www.eset.com
>>
>>
>>

>
> So if I change the one line to below will this be ok? Thanks
>
> Dim dtr As SqlDataReader =
> cmdSel.ExecuteReader(Data.CommandBehavior.CloseCon nection)
>
>

 
Reply With Quote
 
Mr. Arnold
Guest
Posts: n/a
 
      10-12-2009

"David C" <> wrote in message
news:OrWEX%...
>
> "Mr. Arnold" <MR. > wrote in message
> news:...
>>
>> "David C" <> wrote in message
>> news:...
>>>I have some class functions that return SqlDataReader to fill
>>>DropDownLists. Below is example. I have the RETURN line before the
>>>closing of reader and connection. Are these getting closed or is there
>>>another way to do this? For example, if I had it wrapped in a Using
>>>block? I am getting connection pool timeouts and am only testing the site
>>>so I'm not sure why. Thanks in advance.
>>>
>>> David
>>>
>>> Public Shared Function GetWICounties() As SqlDataReader
>>> Dim conData As SqlConnection = New
>>> SqlConnection(ConfigurationManager.ConnectionStrin gs("MCFICoreConnectionString").ConnectionString)
>>> conData.Open()
>>>
>>> Dim strSQL As String
>>>
>>> strSQL = "SELECT CountyID, County" & _
>>> " FROM dbo.WICounties" & _
>>> " ORDER BY County"
>>>
>>> Dim cmdSel As SqlCommand = New SqlCommand(strSQL, conData)
>>> Dim dtr As SqlDataReader = cmdSel.ExecuteReader()
>>> Return dtr
>>> dtr.Close()
>>> conData.Close()
>>> End Function

>>
>>
>> You don't return a datareader, and it looks like you left everything open
>> without closing them before you left.
>>
>> You read the data with a datareader that is reading the data from the
>> resultset at the spot of the returned resultset, you instantiate a DTO
>> (Data Transfer Object of the required properties), you add the DTO
>> to List<T> and you return the List<T> and bind the List<T> (which is
>> bindable) to the control.
>>

>
> So if I change the one line to below will this be ok? Thanks
>
> Dim dtr As SqlDataReader =
> cmdSel.ExecuteReader(Data.CommandBehavior.CloseCon nection)
>


CloseConnection ---- When the command is executed, the associated Connection
object is closed when the associated DataReader object is closed. So, I
don't see this helping you the way you have coded things.

It's the same thing with the Using(connection).

If you leave out of the Using(connection) with a Return to leave the
function before you let the Using(connection) complete normally to close the
connection, then you have left things open like the code above.

If you have done the Return and left the Function before you close things,
then you have the same situation as before.

You can't return a closed datareader that I know about and use it elsewhere,
and that is your problem.

What I have told you about in returning a List<T> of DTO(s) after closing
all connections is what I would call best practice.

I also say if Returning something, then it should be the last statement in
the function.

You instantiate the return of Something as New so it's empty, so either way,
you have return something -- empty or it has data in it that you have
populated.

I also say that it should be in a try/catch/throw when dealing with the
database, another best practice.

This may help you, as I certainly purchased it to help me design better
solutions and code better.

http://www.dofactory.com/Framework/Framework.aspx


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4498 (20091011) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



 
Reply With Quote
 
Gregory A. Beamer
Guest
Posts: n/a
 
      10-12-2009
"David C" <> wrote in
news::

> I have some class functions that return SqlDataReader to fill
> DropDownLists. Below is example. I have the RETURN line before the
> closing of reader and connection. Are these getting closed or is
> there another way to do this? For example, if I had it wrapped in a
> Using block? I am getting connection pool timeouts and am only testing
> the site so I'm not sure why. Thanks in advance.


You cannot close a connection on a DataReader prior to working with the
data. The DataReader is a firehose cursor, from the database
perspective. It makes a nice analogy. When you think of a firehose, it
is running water as long as the valve is open (the connection). When you
close the valve, no more water, even if the fire is still raging.

If you have a routine that returns a DataReader, you are better to feed
the connection from a routine that will also close it, after you finish
using the data.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Reply With Quote
 
Gregory A. Beamer
Guest
Posts: n/a
 
      10-12-2009
"David C" <> wrote in news:um7LwJsSKHA.1280
@TK2MSFTNGP04.phx.gbl:

> Return dtr
> dtr.Close()
> conData.Close()


Oops, I missed this.

The return will return the DataReader prior to closing, so nothing will
be closed here. The rest of my post still applies.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      10-14-2009

"David C" <> wrote in message
news:...
>I have some class functions that return SqlDataReader to fill
>DropDownLists. Below is example. I have the RETURN line before the closing
>of reader and connection. Are these getting closed or is there another way
>to do this? For example, if I had it wrapped in a Using block? I am getting
>connection pool timeouts and am only testing the site so I'm not sure why.
>Thanks in advance.
>
> David
>
> Public Shared Function GetWICounties() As SqlDataReader
> Dim conData As SqlConnection = New
> SqlConnection(ConfigurationManager.ConnectionStrin gs("MCFICoreConnectionString").ConnectionString)
> conData.Open()
>
> Dim strSQL As String
>
> strSQL = "SELECT CountyID, County" & _
> " FROM dbo.WICounties" & _
> " ORDER BY County"
>
> Dim cmdSel As SqlCommand = New SqlCommand(strSQL, conData)
> Dim dtr As SqlDataReader = cmdSel.ExecuteReader()
> Return dtr
> dtr.Close()
> conData.Close()
> End Function


As others have pointed out, you should not be returning a DataReader from a
method as a DataReader uses a "live" connection. If you start passing the
DataReader around and something goes wrong and the prgram doesn't return to
this method, your connection will not close.

Instead, you should capture the data in some object and return that object
from your method (this is exactly what a DataSet was designed for).

Lastly, because you wrote the DataReader's close and the Connection's close
after your return statement, those two lines of code will never be reached.

-Scott


 
Reply With Quote
 
3P
Guest
Posts: n/a
 
      06-01-2010
>
> If you leave out of the Using(connection) with a Return to leave the
> function before you let the Using(connection) complete normally to close
> the connection, then you have left things open like the code above.
>
> If you have done the Return and left the Function before you close
> things, then you have the same situation as before.
>

This is not true. Dispose is always called.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Closing popup window when closing parent window? =?Utf-8?B?Vk1J?= ASP .Net 3 02-15-2007 08:29 AM
Closing the doors 15 minutes before closing. doofus Computer Support 12 06-11-2005 08:20 AM
Re: Opening / Closing SQL Server connections Patrice ASP .Net 3 09-21-2004 11:58 PM
Closing out database connections. Timeouts Do ASP .Net 2 12-09-2003 10:06 AM
Closing child window WITHOUT closing parent thomas Javascript 0 10-23-2003 04:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57