PC Review


Reply
Thread Tools Rate Thread

Is any kind of connection "close" needed here ?

 
 
Abubakar
Guest
Posts: n/a
 
      16th Jun 2009
hi all,
I have the following code:
class dbase {
public SqlDataSource getdatasource()
{
SqlDataSource sq = new SqlDataSource();
sq.ConnectionString = m_connectionstring;
//sq.SelectCommand = "select village_name, school_name from gps";

return sq;

}
}
and some other function that has the following code:

SqlDataSource ds = new dbase().getdatasource();
ds.SelectCommand = mainquery;
Repeater1.DataSource = ds;
Repeater1.DataBind();

After the DataBind, is any kind of Close function needed, or any other code
that should be written to dispose off things?

Thanks,

...ab


 
Reply With Quote
 
 
 
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      16th Jun 2009
Abubakar,

As it runs, you are sure there is no close needed. The System.Data namespace
has no classes that needs disposing of unmanaged resources explicitly, that
is build in all other methods like the close, but that can build in as well.
The TableAdapter has by instance the open and close build in.

Disposing of the objects is done by the Garbage Collector, that is one of
the reasons why it is called managed code

Cor

"Abubakar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> hi all,
> I have the following code:
> class dbase {
> public SqlDataSource getdatasource()
> {
> SqlDataSource sq = new SqlDataSource();
> sq.ConnectionString = m_connectionstring;
> //sq.SelectCommand = "select village_name, school_name from gps";
>
> return sq;
>
> }
> }
> and some other function that has the following code:
>
> SqlDataSource ds = new dbase().getdatasource();
> ds.SelectCommand = mainquery;
> Repeater1.DataSource = ds;
> Repeater1.DataBind();
>
> After the DataBind, is any kind of Close function needed, or any other
> code that should be written to dispose off things?
>
> Thanks,
>
> ..ab
>
>


 
Reply With Quote
 
Abubakar
Guest
Posts: n/a
 
      16th Jun 2009
Thanks for the reply.

Why i asked this is bcuz after a long long time i have come back to the .net
development and now making a project/website in .net. So i wrote some
ado.net code and played with datasets and datareaders and stuff, and my
website started giving maximum-connections-reached sort of errors, when just
about 10 users were using the website. So I looked around on the internet
and found out that the sqlconnection class's Close () method be called when
I'm done using the connection, and should not be left to the gc. So I
corrected my code and I am using sqldatareader also and at all the places I
had used it I called its Close as well. So i was looking at more of my
database related code and found this code that I posted here, and so I
wanted to know if there is something that needs to be done here as well cuz
I couldnt think of any when I looked at it.

My problem is also related to some queries returning large amount of data,
although I have provided ways to my client to filter the data but they still
make mistakes and so now i'm going to intro some paging in those webpages
also.

...ab



"Cor Ligthert[MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Abubakar,
>
> As it runs, you are sure there is no close needed. The System.Data
> namespace has no classes that needs disposing of unmanaged resources
> explicitly, that is build in all other methods like the close, but that
> can build in as well. The TableAdapter has by instance the open and close
> build in.
>
> Disposing of the objects is done by the Garbage Collector, that is one of
> the reasons why it is called managed code
>
> Cor
>
> "Abubakar" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> hi all,
>> I have the following code:
>> class dbase {
>> public SqlDataSource getdatasource()
>> {
>> SqlDataSource sq = new SqlDataSource();
>> sq.ConnectionString = m_connectionstring;
>> //sq.SelectCommand = "select village_name, school_name from gps";
>>
>> return sq;
>>
>> }
>> }
>> and some other function that has the following code:
>>
>> SqlDataSource ds = new dbase().getdatasource();
>> ds.SelectCommand = mainquery;
>> Repeater1.DataSource = ds;
>> Repeater1.DataBind();
>>
>> After the DataBind, is any kind of Close function needed, or any other
>> code that should be written to dispose off things?
>>
>> Thanks,
>>
>> ..ab
>>
>>

>


 
Reply With Quote
 
Gregory A. Beamer
Guest
Posts: n/a
 
      16th Jun 2009
"Abubakar" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> hi all,
> I have the following code:
> class dbase {
> public SqlDataSource getdatasource()
> {
> SqlDataSource sq = new SqlDataSource();
> sq.ConnectionString = m_connectionstring;
> //sq.SelectCommand = "select village_name, school_name from gps";
>
> return sq;
>
> }
> }
> and some other function that has the following code:
>
> SqlDataSource ds = new dbase().getdatasource();
> ds.SelectCommand = mainquery;
> Repeater1.DataSource = ds;
> Repeater1.DataBind();
>
> After the DataBind, is any kind of Close function needed, or any other
> code that should be written to dispose off things?



Tehcnically, no, as ASP.NET handles it for you. BUT, I would still do a
ds.Dispose(), as it is an explicit indicator you are no longer using the
SqlDataSource and indicates, to the .NET CLR, the object can be cleaned
up.

If the code hangs after this, you will allow the connection to go back
to the pool to be used by another page hit (most likely from another
visitor).


--
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
 
      16th Jun 2009
"Cor Ligthert[MVP]" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Abubakar,
>
> As it runs, you are sure there is no close needed. The System.Data
> namespace has no classes that needs disposing of unmanaged resources
> explicitly, that is build in all other methods like the close, but
> that can build in as well. The TableAdapter has by instance the open
> and close build in.
>
> Disposing of the objects is done by the Garbage Collector, that is one
> of the reasons why it is called managed code



While I agree with this technically, doing an explicit Dispose() on the
SqlDataSource is good form. If there is extensive code after the bind,
the connection would not return to the pool until the code running was
finished. By marking for delete, the connection could be returned to the
pool even while code was running.

For most ASP.NET apps, this is not an issue, of course, as you bind and
display. But adding an explicit Dispose() comes with no cost. There is,
of course, no Close() method on the DataSource objects.


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

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

*******************************************
| Think outside the box! |
*******************************************
 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      16th Jun 2009
Gregory,

I was writing about System.Data, I missed completely that it is about
system.Web, I thought that it was about AdoNet

In system.Data the close of the connectionstring has the disposing done
intrinsic, like Pablo has often in past written in this newsgroup and with
that the releasing of the connection pool.

However, at least the SharePoint object are simply wrappers around com, so
with Web I would be a little bit more careful (like you wrote in fact too)

Cor

"Gregory A. Beamer" <(E-Mail Removed)> wrote in message
news:Xns9C2C5AAFC4519gbworld@207.46.248.16...
> "Cor Ligthert[MVP]" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>> Abubakar,
>>
>> As it runs, you are sure there is no close needed. The System.Data
>> namespace has no classes that needs disposing of unmanaged resources
>> explicitly, that is build in all other methods like the close, but
>> that can build in as well. The TableAdapter has by instance the open
>> and close build in.
>>
>> Disposing of the objects is done by the Garbage Collector, that is one
>> of the reasons why it is called managed code

>
>
> While I agree with this technically, doing an explicit Dispose() on the
> SqlDataSource is good form. If there is extensive code after the bind,
> the connection would not return to the pool until the code running was
> finished. By marking for delete, the connection could be returned to the
> pool even while code was running.
>
> For most ASP.NET apps, this is not an issue, of course, as you bind and
> display. But adding an explicit Dispose() comes with no cost. There is,
> of course, no Close() method on the DataSource objects.
>
>
> --
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
>
> Twitter: @gbworld
> Blog: http://gregorybeamer.spaces.live.com
>
> *******************************************
> | Think outside the box! |
> *******************************************


 
Reply With Quote
 
Abubakar
Guest
Posts: n/a
 
      17th Jun 2009
ok cool. Thanks.

"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Abubakar" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> After the DataBind, is any kind of Close function needed, or any other
>> code that should be written to dispose off things?

>
> Yes, definitely!
>
> Although the garbage collector will (eventually) clean up managed
> resources when they fall out of scope, you have very little control over
> when that happens unless you call GC.Collect() explicitly. Contrary to
> popular opinion, the garbage collector does not automatically clean up
> managed resources as soon as they fall out of scope. Therefore, you might
> think that calling GC.Collect() all the time is a good idea, but it
> isn't - this will cause the garbage collector to collect *everything*
> which has fallen out of scope, not just the resources that have fallen out
> of scope of a particular piece of code, which is almost certainly
> extremely inefficient... See this page for a discussion of this:
> http://forums.whirlpool.net.au/forum...fm/674562.html
>
> You could use a try {} catch {} finally {} model where you dispose the
> various ADO.NET objects in the finally {} section. This has the added
> advantage that the objects will still be disposed even if an error occurs
> in the try {} section. As it stands, your code has no exception handling
> at all. This means that it is susceptible to memory leaks. E.g.
>
> SqlDataSource ds = new dbase().getdatasource();
> ds.SelectCommand = mainquery;
> Repeater1.DataSource = ds;
> Repeater1.DataBind();
>
> If an exception is thrown for any reason by the second, third or fourth
> lines above, the SqlDataSource object will remain in memory until the
> garbage collector decides that it can clean it up. As stated above, this
> may not be straightaway. This might not be too much of an issue in a
> WinForms app, but in an ASP.NET with lots of concurrent users it could
> potentially cause the web app to crash.
>
> See here for an example of the SqlDataSource's Dispose() method being
> called in a finally {} section:
> http://msdn.microsoft.com/en-us/library/55b58dye.aspx The accompanying
> code comment "// If anything strange happens, clean up" pretty much says
> it all...
>
> You can also use the using keyword for this scenario:
> http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx
>
> using (SqlDataSource ds = new dbase().getdatasource())
> {
> ds.SelectCommand = mainquery;
> Repeater1.DataSource = ds;
> Repeater1.DataBind();
> }
>
> However, although this means that the SqlDataSource object will be
> collected even if an error occurs in the code contained within it, you
> should still consider using proper exception handling.
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net


 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      19th Jun 2009
Abubakar wrote:
> Why i asked this is bcuz after a long long time i have come back to
> the .net development and now making a project/website in .net. So i
> wrote some ado.net code and played with datasets and datareaders and
> stuff, and my website started giving maximum-connections-reached sort
> of errors, when just about 10 users were using the website.


Is the error being raised by IIS or by your code?

If you're using IIS on XP, there is a 10 connection limit to IIS.

Andrew


 
Reply With Quote
 
Abubakar
Guest
Posts: n/a
 
      22nd Jun 2009
Using IIS on windows server 2003.

"Andrew Morton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Abubakar wrote:
>> Why i asked this is bcuz after a long long time i have come back to
>> the .net development and now making a project/website in .net. So i
>> wrote some ado.net code and played with datasets and datareaders and
>> stuff, and my website started giving maximum-connections-reached sort
>> of errors, when just about 10 users were using the website.

>
> Is the error being raised by IIS or by your code?
>
> If you're using IIS on XP, there is a 10 connection limit to IIS.
>
> Andrew
>


 
Reply With Quote
 
William Vaughn MVP
Guest
Posts: n/a
 
      22nd Jun 2009
Ah, no. NEVER depend on the GC to close a connection. If you do, your
application will fail once the connection pool fills.
This is especially true for ASP.NET applications. "If it works, it must be
right" (you must be kidding). That's like saying a bridge is safe after a
single Volkswagen beetle drives over it at 20 MPH.

--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
http://betav.com http://betav.com/blog/billva
____________________________________________________________________________________________



"Cor Ligthert[MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Abubakar,
>
> As it runs, you are sure there is no close needed. The System.Data
> namespace has no classes that needs disposing of unmanaged resources
> explicitly, that is build in all other methods like the close, but that
> can build in as well. The TableAdapter has by instance the open and close
> build in.
>
> Disposing of the objects is done by the Garbage Collector, that is one of
> the reasons why it is called managed code
>
> Cor
>
> "Abubakar" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> hi all,
>> I have the following code:
>> class dbase {
>> public SqlDataSource getdatasource()
>> {
>> SqlDataSource sq = new SqlDataSource();
>> sq.ConnectionString = m_connectionstring;
>> //sq.SelectCommand = "select village_name, school_name from gps";
>>
>> return sq;
>>
>> }
>> }
>> and some other function that has the following code:
>>
>> SqlDataSource ds = new dbase().getdatasource();
>> ds.SelectCommand = mainquery;
>> Repeater1.DataSource = ds;
>> Repeater1.DataBind();
>>
>> After the DataBind, is any kind of Close function needed, or any other
>> code that should be written to dispose off things?
>>
>> Thanks,
>>
>> ..ab
>>
>>

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Re: "Close connection" or "Dispose connection" bruce barker Microsoft ASP .NET 0 29th Oct 2009 02:26 AM
Re: "Close connection" or "Dispose connection" Scott M. Microsoft ASP .NET 0 28th Oct 2009 04:01 PM
"Generic Host Process for Win32 needed to close" error =?Utf-8?B?YmlsbGQ=?= Windows XP Help 3 21st Feb 2005 11:11 AM
"FireDaemon.EXE encountered a problem and needed to close" Philip Dartnell Windows XP Networking 1 3rd Oct 2003 07:10 PM
kind of cable needed for 5 1/4" ? Marcel Verduyn Computer Hardware 4 25th Sep 2003 03:19 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:55 PM.