Groups | Blog | Home
all groups > dotnet ado.net > january 2007 >

dotnet ado.net : leave connections open for how long?


Andrew Robinson
1/11/2007 3:01:17 PM
I recently got into a discussion with a coworker about just how long to
leave a connection open. I have always opened as late as I can and closed at
the earliest possible point in time.

using (SqlConnection cn = new SqlConnection(DataConnection))
using (SqlCommand cm = new SqlCommand(InsertCommandText, cn))
{
cm.CommandType = InsertCommandType;

cn.Open();
cm.ExecuteNonQuery();
cn.Close();
}

Is there any reason other than executing within a transaction to leave a
connection open? What if I am executing a series of commands outside of a
transaction? Can anyone point me to any benchmarks on this?

Thanks,
William (Bill) Vaughn
1/11/2007 6:29:29 PM
And then there is Scenario 2.5:
Your company has a LAN and it has to support a thousand (typically far
fewer) active connections as applications come and go during the day. In
this case since SS can handle these connections with ease, it's fine to open
and keep a large number of connections open indefinitely.

And Scenario 3.0
Your company supports a web site which is running ASP applications. In
this case the way that the applications are instantiated and torn down after
they're used dictate that you open and quickly close connections as they are
used. In this case the connections are handled by the Connection Pool which
is used to hold a limited number of connections open so the overhead of
connecting is minimized.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

[quoted text, click to view]

Andrew Robinson
1/12/2007 2:20:44 PM
Bill,

This is an asp.net app so lets call it scenario 3.0 and I understand that we
want to tie up the thread pool for as short a period of time as possible.
But, if I am going to execute 2 or 3 "command.ExecuteQuery()" methods (one
after the next), are you still saying that we should open and close between
each of them?

I tend to say but not sure that is based on a performance requirement but
more just on a best practice standpoint. You never know when you will get
bumped out of the current thread pool and by slicing in as fine grained a
programming model as possible we are allowing the system to perform at its
best.

Pretty sure I know the answer here but will feel better knowing the king of
all things sql agrees.


Thanks again.

-Andy

[quoted text, click to view]
Stephany Young
1/12/2007 2:35:52 PM
It is all a matter of choice, but there a number of factors tht you would
need to consider when making that choice, for example:

Scenario 1:

Your application 'talks' to Sql Server (maybe Express) on the same machine
and no other application accesses that Sql Server at all.

In this case there is never going to be any contention for connection
resources and it would be quite safe to open the connection once and leave
it open.

Scenario 2:

Your application 'talks' to Sql Server on a networked server and that
application is installed and in continual use on 10000 other machines on the
networks all 'talking' to the same Sql Server.

In this case there is going to be contention for connection resources and it
would be probably be quite unsafe to open the connection once and leave it
open.


As you can see there are best case and worst case scenarios and there will
be a series of 'shades' in between.

As far as benchmarks are concerned, simply time how long it takes to connect
the first time and then time how long it takes to connect subsequently,
Because of connection pooling and other factors I think you will find that
there is little significant overhead in this respect but the time it takes
to connect is only one factor.


[quoted text, click to view]

William (Bill) Vaughn
1/12/2007 3:30:04 PM
Not at all (while some would say you should). If your code is well written
and can't leak connections (falling into an exception handler and not
closing) then executing several methods on the same connection is fine--and
IMHO should yield better performance. Opening connections is not free--even
from the Connection Pool. If there is a chance that you'll get bumped out,
the only way might be to wrap the connections in a Using scope. However, I
doubt if any swapping would be permitted to break into an active connection
operation. If it did, we would have a lot more overflowing pools than we
have nowadays.



--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

[quoted text, click to view]

Cor Ligthert [MVP]
1/13/2007 3:09:46 PM
Andrew,

I don't agree with the previous writers. As you can avoid to create a
program for a special scale, than avoid it. As it has no big consequences
for the real performance, than build your application for a large scale. If
you are using one connection in a program, than you can always rescale it,
but it will be a lot of work, that is needles done if you had in mind direct
an environment from that scale.

This is of course not if you have to do much additions for your code. But
that is in my idea never. By instance a dataadapter will automatically open
and close a connection for you, if you have not closed before.

Have you any idea why that was done already as a standard?

Cor

"Andrew Robinson" <nemoby@nospam.nospam> schreef in bericht
news:%23sAcGSdNHHA.4928@TK2MSFTNGP06.phx.gbl...
[quoted text, click to view]

William (Bill) Vaughn
1/13/2007 4:10:15 PM
Again, this is true for ASP--not necessarily for connected Windows Forms
applications and not at all for SQLCe applications or other single-user DBMS
engine applications.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

[quoted text, click to view]

Miha Markic [MVP C#]
1/14/2007 12:12:12 AM
The definition is open as late as possible and close it as soon as possible.
That doesn't imply that you should open and close connection for each
command. It rather implies that you have to open the connection right before
doing database operations and close it asap you finished with database
operation*s*.
Opening and closing for each operation doesn't make sense.
--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

[quoted text, click to view]
Cor Ligthert [MVP]
1/14/2007 5:56:18 AM
Bill,

Beside technical reasons are there as well "maintenance reasons".

Those are for me always much more important than any technical benefit.
(They seldom confict in a way that it takes seconds).

The open and close in the way as Miha describes is one of those I prefer
because of the sentence above.

Just my opinion.

Cor

"William (Bill) Vaughn" <billvaRemoveThis@nwlink.com> schreef in bericht
news:OzQ3iB3NHHA.1248@TK2MSFTNGP03.phx.gbl...
[quoted text, click to view]
Miha Markic [MVP C#]
1/15/2007 10:04:29 AM

[quoted text, click to view]

It is true for WinForms also, as long as you want a responsive application.

and not at all for SQLCe applications or other single-user DBMS
[quoted text, click to view]

I tend to disagree. It is true when you are multithreading - not sure on how
SQLCe fits in this scenario - does it allow multiple connections? I guess
yes.
Anyway I was commenting on the definition open late, close early and not
that this rule applies to all situations.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
Andrew Robinson
1/23/2007 9:32:42 AM
Thanks for all the info. As with most things in the programming world, it
appears that there are a number of opinions on how this should be done. It
feels good to know that even though there is no single correct answer, I am
within the spread.

-Andy

[quoted text, click to view]
AddThis Social Bookmark Button