PC Review


Reply
Thread Tools Rate Thread

Connection Pooling and Preparing Commands

 
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      22nd Jun 2006
Hi Folks

I am just trying to get my head around whether I can use a single
SQLConnection for the life of the application or whether I should create it
only when needed.

I want to create cached SQLCommand objects that are then prepared to reduce
subsequent execution time, but these can't be prepared unless their
Connection property has been set.

This then causes me to think that, if I create/dispose connections in a
"using" block, the vommands are going to keep a reference to the connection
alive anyway, so why not just keep the connection alive for the duration of
the application ?

If I make a connection for the first time, then prepare the commands, then
call Dispose on the connection, will the commands have to be prepared the
next time I instantiate the connection, using pooling ?

So I have two options :

1. Create connection, prepare commands on first call then keep them in a
list, only close connection when app closes

2. Create connection in "using" block, this then means I don't keep a list
of prepared commands and have to take the speed hit on running the commands
every time I get a new connection.

What is the better route ?

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
 
 
 
ChrisM
Guest
Posts: n/a
 
      22nd Jun 2006
Hi Joanna,

I'm no expert, so don't quote me on this... :-) but my understanding is that
it really depends on the number of users, the regularity of the calls, and
the importance of speedy responses.
That is if you have 100 users that are making infrequent calls to the
database then you'd be better off opening and closing connections as
required.
If you have 1-2 users that are making regular calls then it makes sense to
open a connection, and keep it open for the duration of the application.

Anything in-between I guess is a judgement call between the two...??

I'll watch this thread with interest as I'm never 100% sure of the right way
to go on this...

Cheers,

ChrisM.


"Joanna Carter [TeamB]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Folks
>
> I am just trying to get my head around whether I can use a single
> SQLConnection for the life of the application or whether I should create
> it
> only when needed.
>
> I want to create cached SQLCommand objects that are then prepared to
> reduce
> subsequent execution time, but these can't be prepared unless their
> Connection property has been set.
>
> This then causes me to think that, if I create/dispose connections in a
> "using" block, the vommands are going to keep a reference to the
> connection
> alive anyway, so why not just keep the connection alive for the duration
> of
> the application ?
>
> If I make a connection for the first time, then prepare the commands, then
> call Dispose on the connection, will the commands have to be prepared the
> next time I instantiate the connection, using pooling ?
>
> So I have two options :
>
> 1. Create connection, prepare commands on first call then keep them in a
> list, only close connection when app closes
>
> 2. Create connection in "using" block, this then means I don't keep a list
> of prepared commands and have to take the speed hit on running the
> commands
> every time I get a new connection.
>
> What is the better route ?
>
> --
> Joanna Carter [TeamB]
> Consultant Software Engineer
>
>



 
Reply With Quote
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      22nd Jun 2006
"ChrisM" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...

| I'm no expert, so don't quote me on this... :-)

Yes, we are all learning...; and how ! :-)

| That is if you have 100 users that are making infrequent calls to the
| database then you'd be better off opening and closing connections as
| required.

That is what I had planned on doing, but I understood that connection
pooling required you to not only open/close the connection but to
create/open/close/dispose it on every call.

I am reall trying to find out if creating a connection, and keeping the
connection alive but not necessarily opening/closing it until required, only
disposing it when the app closes, is a "proper" way to use a connection.

If I can keep the connection alive, but not necessarily open, for the length
of program execution (all day), then I could prepare commands and keep them
in a cache to save execution time there.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      23rd Jun 2006
Hi,


"Joanna Carter [TeamB]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Folks
>
> I am just trying to get my head around whether I can use a single
> SQLConnection for the life of the application or whether I should create
> it
> only when needed.


IMO, you should create it at the last moment before connecting to the DB and
destroy it afterwards.

> I want to create cached SQLCommand objects that are then prepared to
> reduce
> subsequent execution time, but these can't be prepared unless their
> Connection property has been set.


How complex these Commands are? Most probably you do not need to call
Prepare. Please note that this is my personal opinion and not one respalded
with tests.
Also note that if you are calling a SP the Prepare has no impact as the
query is already "prepared" in the server.

Somewhere I read than SQL 2005 ( or 2000 ) had such a good query preparer
(not sure that is the correct term) than create the execution path of a
query was not longer a big problem, unless a really complex query was being
used.


IMO unless you are getting bad results you should stick with the create the
later, release the sooner.


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



 
Reply With Quote
 
=?Utf-8?B?Um9u?=
Guest
Posts: n/a
 
      24th Jun 2006
When using connection pooling, closing the connection does not dispose the
object but returns it to the pool. Conversely, when instantiating a new
connection you are not really creating a new one but retrieving it from the
pool. This is highly optimized already, and it's better than keeping a
connection alive for the life of the application.

Ron

"Joanna Carter [TeamB]" wrote:

> "ChrisM" <(E-Mail Removed)> a écrit dans le message de news:
> (E-Mail Removed)...
>
> | I'm no expert, so don't quote me on this... :-)
>
> Yes, we are all learning...; and how ! :-)
>
> | That is if you have 100 users that are making infrequent calls to the
> | database then you'd be better off opening and closing connections as
> | required.
>
> That is what I had planned on doing, but I understood that connection
> pooling required you to not only open/close the connection but to
> create/open/close/dispose it on every call.
>
> I am reall trying to find out if creating a connection, and keeping the
> connection alive but not necessarily opening/closing it until required, only
> disposing it when the app closes, is a "proper" way to use a connection.
>
> If I can keep the connection alive, but not necessarily open, for the length
> of program execution (all day), then I could prepare commands and keep them
> in a cache to save execution time there.
>
> Joanna
>
> --
> Joanna Carter [TeamB]
> Consultant Software Engineer
>
>
>

 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      24th Jun 2006
You don't gain anything by keeping a Connection object that is not
connected to the database. That object doesn't contain the actual
connection, it's just like an empty shell.

The Connection object gets a connection from the pool when you call the
Open method, and return it to the pool when you call the Close method.

(And, no, keeping a connected Connection objects is not a good idea either.)

Joanna Carter [TeamB] wrote:
> "ChrisM" <(E-Mail Removed)> a écrit dans le message de news:
> (E-Mail Removed)...
>
> | I'm no expert, so don't quote me on this... :-)
>
> Yes, we are all learning...; and how ! :-)
>
> | That is if you have 100 users that are making infrequent calls to the
> | database then you'd be better off opening and closing connections as
> | required.
>
> That is what I had planned on doing, but I understood that connection
> pooling required you to not only open/close the connection but to
> create/open/close/dispose it on every call.
>
> I am reall trying to find out if creating a connection, and keeping the
> connection alive but not necessarily opening/closing it until required, only
> disposing it when the app closes, is a "proper" way to use a connection.
>
> If I can keep the connection alive, but not necessarily open, for the length
> of program execution (all day), then I could prepare commands and keep them
> in a cache to save execution time there.
>
> Joanna
>

 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      24th Jun 2006

"Ron" <(E-Mail Removed)> wrote in message
news:92496D3E-E4C7-4D65-8230-(E-Mail Removed)...
| When using connection pooling, closing the connection does not dispose the
| object but returns it to the pool. Conversely, when instantiating a new
| connection you are not really creating a new one but retrieving it from
the
| pool. This is highly optimized already, and it's better than keeping a
| connection alive for the life of the application.
|

In principle, there is nothing wrong with keeping a single connection open
for the duration of an application (that's what the pool manager does after
all), I guess in Joanna's case it's the desired behavior. Keep in mind that
the Connection pool manager closes the connection with the database after
the connection has been idle for a certain amount of time (~6 minutes), so
if you only happen to issue commands every > 6 minutes you'll pay the
physical connection and athentication overhead for each open. Connection
pooling is great fo "server" style applications where a single application
(process) may have a varying number of DB connections open at a time.
"client" style applications that only have one single DB connection open at
a time do not take that much advantage of connection pooling.


Willy.




 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      24th Jun 2006
Willy Denoyette [MVP] wrote:
> "Ron" <(E-Mail Removed)> wrote in message
> news:92496D3E-E4C7-4D65-8230-(E-Mail Removed)...
> | When using connection pooling, closing the connection does not dispose the
> | object but returns it to the pool. Conversely, when instantiating a new
> | connection you are not really creating a new one but retrieving it from
> the
> | pool. This is highly optimized already, and it's better than keeping a
> | connection alive for the life of the application.
> |
>
> In principle, there is nothing wrong with keeping a single connection open
> for the duration of an application


For Windows applications, yes. For web applications, no.

> (that's what the pool manager does after
> all), I guess in Joanna's case it's the desired behavior. Keep in mind that
> the Connection pool manager closes the connection with the database after
> the connection has been idle for a certain amount of time (~6 minutes), so
> if you only happen to issue commands every > 6 minutes you'll pay the
> physical connection and athentication overhead for each open. Connection
> pooling is great fo "server" style applications where a single application
> (process) may have a varying number of DB connections open at a time.
> "client" style applications that only have one single DB connection open at
> a time do not take that much advantage of connection pooling.
>
>
> Willy.
>
>
>
>

 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      24th Jun 2006
That's what I said "Connection pooling is great fo "server" style
applications...".
Web applications are "server style".

Willy.

"Göran Andersson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| Willy Denoyette [MVP] wrote:
| > "Ron" <(E-Mail Removed)> wrote in message
| > news:92496D3E-E4C7-4D65-8230-(E-Mail Removed)...
| > | When using connection pooling, closing the connection does not dispose
the
| > | object but returns it to the pool. Conversely, when instantiating a
new
| > | connection you are not really creating a new one but retrieving it
from
| > the
| > | pool. This is highly optimized already, and it's better than keeping a
| > | connection alive for the life of the application.
| > |
| >
| > In principle, there is nothing wrong with keeping a single connection
open
| > for the duration of an application
|
| For Windows applications, yes. For web applications, no.
|
| > (that's what the pool manager does after
| > all), I guess in Joanna's case it's the desired behavior. Keep in mind
that
| > the Connection pool manager closes the connection with the database
after
| > the connection has been idle for a certain amount of time (~6 minutes),
so
| > if you only happen to issue commands every > 6 minutes you'll pay the
| > physical connection and athentication overhead for each open. Connection
| > pooling is great fo "server" style applications where a single
application
| > (process) may have a varying number of DB connections open at a time.
| > "client" style applications that only have one single DB connection open
at
| > a time do not take that much advantage of connection pooling.
| >
| >
| > Willy.
| >
| >
| >
| >


 
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
How can I force connection pooling to keep at least one connection alive ? TheSteph Microsoft C# .NET 6 22nd Nov 2007 03:25 PM
Connection pooling parameters not working for Oracle connection yoram.ayalon@structuredweb.com Microsoft ADO .NET 1 29th Sep 2006 09:36 PM
Connection Pooling and Preparing Commands Joanna Carter [TeamB] Microsoft ADO .NET 6 25th Jun 2006 04:10 PM
connection in connection pool with pooling=false Jason Collins Microsoft ADO .NET 10 22nd Jun 2004 01:21 PM
How do I turn off connection pooling in a connection string corbett Microsoft Dot NET 1 6th Jan 2004 11:36 PM


Features
 

Advertising
 

Newsgroups
 


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