Is any kind of connection "close" needed here ?

A

Abubakar

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
 
C

Cor Ligthert[MVP]

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
 
A

Abubakar

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
 
G

Gregory A. Beamer

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! |
*******************************************
 
G

Gregory A. Beamer

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! |
*******************************************
 
C

Cor Ligthert[MVP]

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
 
A

Abubakar

ok cool. Thanks.

Mark Rae said:
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-replies-archive.cfm/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.
 
A

Andrew Morton

Abubakar said:
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
 
A

Abubakar

Using IIS on windows server 2003.

Andrew Morton said:
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
 
W

William Vaughn MVP

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
____________________________________________________________________________________________
 
A

Abubakar

right" (you must be kidding). That's like saying a bridge is safe after a
single Volkswagen beetle drives over it at 20 MPH.

:)
ok

btw i have modified the code with all the "close" being called asa (as soon
as) the data was not being read anymore, application is running pretty good
now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top