HighTechTalks DotNet Forums  

Caching a sqlconnection

Dotnet General Discussions microsoft.public.dotnet.general


Discuss Caching a sqlconnection in the Dotnet General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Rbrt
 
Posts: n/a

Default Caching a sqlconnection - 11-16-2007 , 10:13 AM






I am developing a asp.net 2.0 app using vb and am thinking about using the
system cache to cache a sqlconnection. However, I am concerned there might be
implications to this which are not obvious.

Can anybody offer any thoughts on whetehr this might be a good or bad idea?

Thanks

Robert

Reply With Quote
  #2  
Old   
Marc Gravell
 
Posts: n/a

Default Re: Caching a sqlconnection - 11-16-2007 , 10:30 AM






Don't bother. Just ensure you close and dispose your connections after
every use, and let the connection-pool do its job. Closing a
SqlConnection doesn't close the *actual* connection - it just releases
it to the pool for re-use.

Marc



Reply With Quote
  #3  
Old   
Rbrt
 
Posts: n/a

Default Re: Caching a sqlconnection - 11-16-2007 , 10:50 AM



The reason I am interested in doing it is that it saves code. To open a
connection, I have to go to the trouble of setting the connection string,
catching and handling exceptions....

To get it from the cache, I just

dim conn as sqlconnection = new sqlconnection
if not isnothing(cache("conn")) then
conn=cache("conn")
else
set up conn, open conn, handle errors, cache conn...
end if

"Marc Gravell" wrote:

Quote:
Don't bother. Just ensure you close and dispose your connections after
every use, and let the connection-pool do its job. Closing a
SqlConnection doesn't close the *actual* connection - it just releases
it to the pool for re-use.

Marc




Reply With Quote
  #4  
Old   
Marc Gravell
 
Posts: n/a

Default Re: Caching a sqlconnection - 11-16-2007 , 11:02 AM



in asp.net? that means that every request is going to be trying to use
*the same* connection. Not even "MARS" is that good...
Seriously, people have spent a lot of time getting pooling "right"...
and it isn't any more code:

using(SqlConnection conn = new SqlConnection(someString)) {
conn.Open();
}

that's it! I'm sure VB.NET has a "using" equivalent... (I'm a C# guy
so can't say 100%)

Marc



Reply With Quote
  #5  
Old   
Rbrt
 
Posts: n/a

Default Re: Caching a sqlconnection - 11-16-2007 , 11:19 AM



I have a "utility" connection in the code that is used for performing
"plumbing" type tasks - building controls, logging error messages, reading
system parameters from a parameter table, logging in a user, etc... That is
the connection I would be caching.

However, your comment tells me that I might be overloading the connection
when there are many sessions so that is the sort of thing I was wondering
about.

Thanks.

"Marc Gravell" wrote:

Quote:
in asp.net? that means that every request is going to be trying to use
*the same* connection. Not even "MARS" is that good...
Seriously, people have spent a lot of time getting pooling "right"...
and it isn't any more code:

using(SqlConnection conn = new SqlConnection(someString)) {
conn.Open();
}

that's it! I'm sure VB.NET has a "using" equivalent... (I'm a C# guy
so can't say 100%)

Marc




Reply With Quote
  #6  
Old   
Miro
 
Posts: n/a

Default Re: Caching a sqlconnection - 11-16-2007 , 12:40 PM



I thought I read that connection pooling is automatic - ( not using mars ), that if
all the parameters are the same as what the last connection pool was, it uses the
connection from the pool.

Change anything - even as far as a variable name, its considered a new connection.

So the trick is to always use the same "my.connection" setting or whatever ur variable is.

I may have read it wrong / and be completely wrong - im a newbie.

M.

Rbrt wrote:
Quote:
I have a "utility" connection in the code that is used for performing
"plumbing" type tasks - building controls, logging error messages, reading
system parameters from a parameter table, logging in a user, etc... That is
the connection I would be caching.

However, your comment tells me that I might be overloading the connection
when there are many sessions so that is the sort of thing I was wondering
about.

Thanks.

"Marc Gravell" wrote:

in asp.net? that means that every request is going to be trying to use
*the same* connection. Not even "MARS" is that good...
Seriously, people have spent a lot of time getting pooling "right"...
and it isn't any more code:

using(SqlConnection conn = new SqlConnection(someString)) {
conn.Open();
}

that's it! I'm sure VB.NET has a "using" equivalent... (I'm a C# guy
so can't say 100%)

Marc




Reply With Quote
  #7  
Old   
Marc Gravell
 
Posts: n/a

Default Re: Caching a sqlconnection - 11-16-2007 , 04:19 PM



Rbrt> I have a "utility" connection in the code that is used for
performing "plumbing" type tasks
cache the connection string; let the connection-pool worry about the
rest. Honestly, this is the best approach. Otherwise you will have
concurrency and/or scalability issues.

Miro> I thought I read that connection pooling is automatic <snip>
Your understanding is correct; it simply compares the connection
string. Note that pooling can be disabled, and isn't enabled by
default on all providers - but with SqlClient it is.

Marc

Reply With Quote
  #8  
Old   
Marc Gravell
 
Posts: n/a

Default Re: Caching a sqlconnection - 11-16-2007 , 04:19 PM



(and when I say "cache", I mean for instance in a static field, not
the asp.net cache)

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.