PC Review


Reply
Thread Tools Rate Thread

Connection pooling question

 
 
Kirsten
Guest
Posts: n/a
 
      11th Aug 2009
Suppose I open and close the connection like this:

SqlConnection conn = new SqlConnection(myConnectionString);
try
{
conn.Open();
...doSomething;
}
finally
{
conn.Close();
}

where connection pooling is enabled by default (I mean: I specify nothing in
the connection string like pool size, etc).

- Is the connection to SQL Server actually closed in "conn.Close()" or conn
stays in ConnectionState.Closed and the connection is added to the pool as
available?
- Should I call "conn.Close()" or let .NET close the connection as needed?

Thanks!

 
Reply With Quote
 
 
 
 
William Vaughn \(MVP\)
Guest
Posts: n/a
 
      11th Aug 2009
Ah, when you don't disable connection pooling in the connectionstring, when
you call Close, the connection to the backend (SQL Server in this case) is
left open for 4-8 minutes. When another instance of the same application (in
the ASP.NET app domain) uses the same connection string, the connection
handle (of the open connection) is passed to the application and at that
point the connection is flushed of its previous state and made available.

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



"Kirsten" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> Suppose I open and close the connection like this:
>
> SqlConnection conn = new SqlConnection(myConnectionString);
> try
> {
> conn.Open();
> ...doSomething;
> }
> finally
> {
> conn.Close();
> }
>
> where connection pooling is enabled by default (I mean: I specify nothing
> in the connection string like pool size, etc).
>
> - Is the connection to SQL Server actually closed in "conn.Close()" or
> conn stays in ConnectionState.Closed and the connection is added to the
> pool as available?
> - Should I call "conn.Close()" or let .NET close the connection as needed?
>
> Thanks!


 
Reply With Quote
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      12th Aug 2009
"William Vaughn (MVP)" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> Ah, when you don't disable connection pooling in the connectionstring,
> when you call Close, the connection to the backend (SQL Server in this
> case) is left open for 4-8 minutes. When another instance of the same
> application (in the ASP.NET app domain) uses the same connection string,
> the connection handle (of the open connection) is passed to the
> application and at that point the connection is flushed of its previous
> state and made available.



This is my understanding, as well, although I thought the timeout period
from the pool was much shorter than 4-8 minutes. Where is that documented?

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

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

********************************************************
| Think outside the box! |
********************************************************

 
Reply With Quote
 
William Vaughn \(MVP\)
Guest
Posts: n/a
 
      14th Aug 2009
I got this figure from Pablo Castro at Microsoft some time ago. I have
verified it several times.
__________________________________________________________________________
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
____________________________________________________________________________________________



"Cowboy (Gregory A. Beamer)" <(E-Mail Removed)> wrote in
message news:8C2D1607-B0E0-423A-AAB0-(E-Mail Removed)...
> "William Vaughn (MVP)" <(E-Mail Removed)> wrote in message
> news:#(E-Mail Removed)...
>> Ah, when you don't disable connection pooling in the connectionstring,
>> when you call Close, the connection to the backend (SQL Server in this
>> case) is left open for 4-8 minutes. When another instance of the same
>> application (in the ASP.NET app domain) uses the same connection string,
>> the connection handle (of the open connection) is passed to the
>> application and at that point the connection is flushed of its previous
>> state and made available.

>
>
> This is my understanding, as well, although I thought the timeout period
> from the pool was much shorter than 4-8 minutes. Where is that documented?
>
> --
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
>
> Twitter: @gbworld
> Blog: http://gregorybeamer.spaces.live.com
>
> ********************************************************
> | Think outside the box! |
> ********************************************************


 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      29th Sep 2009
Or if you cannot close the connection immediately implement the dispose
pattern and use
CommandBehavior.CloseConnection on datareaders.





"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Kirsten" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>
>> Suppose I open and close the connection like this:

>
> In addition to Bill's and Greg's responses, I would suggest that you do
> this instead:
>
> using (SqlConnection conn = new SqlConnection(myConnectionString))
> {
> try
> {
> conn.Open();
> ...doSomething;
> }
> catch (Exception ex)
> {
> // handle the exception...
> }
> }
>
> http://msdn.microsoft.com/en-us/library/yh598w02.aspx
> http://davidhayden.com/blog/dave/arc...01/13/773.aspx
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net



 
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
Connection pooling question fniles Microsoft VB .NET 20 19th Apr 2007 03:03 PM
Question on connection pooling Steve Franks Microsoft ASP .NET 7 28th Oct 2005 07:15 PM
Connection pooling question. Frank Rizzo Microsoft ADO .NET 6 13th Jul 2005 06:13 PM
A question on Connection Pooling when using sockets Vinod Microsoft ASP .NET 2 27th Feb 2004 12:15 PM
Connection pooling - question Juan Ignacio Gelos Microsoft ADO .NET 1 15th Nov 2003 11:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:18 PM.