Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Database Access / December 2007



Tip: Looking for answers? Try searching our database.

Keep ADO connection or not

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chris Doucé - 10 Dec 2007 21:32 GMT
Hi all,

This question has probably been answerd before but I don't find an
satisfying answer in Google.

What is best?
Open the connection when the application loads and keep it open or

Open the connection just when I need it and close it immediately after the
dbase operation.

Thanks for the advise

Chris
Ralph - 10 Dec 2007 22:43 GMT
> Hi all,
>
[quoted text clipped - 8 lines]
>
> Thanks for the advise

The latter.

Why your having trouble finding a satisfactory answer is probably over what
a particular author means by a "connection". When a client creates an
initial connection by creating an ADODB.Connection object, this is where all
the heavy work gets done. The ADO library seeks out the requested provider,
validates it, then seeks out the database, wakes up the engine, ... in short
assembles a pathway from your client to the database thru all the
intervening components (or wire if involved).

It then setups a 'connection pool' with one connection. When you call .Open
on the ADODB.Connection it then pulls an available connection from the pool.
If one is busy it creates another one for you. This part is quick. Think of
the initial connection as precompiled and ready of use at a moments notice.

Also once you have created an ADODB.Connection object, if you create another
one using the same primary elements of the previous Connection String - you
get the same pool as the first. (So you can have mulitple Connection
objects, yet only one 'connection')

So the reason to keep one connection around is to avoid the expense of
re-creating a connection, but the ADO library is doing this for you. There
is always one available in the pool.

Also note that connections are automatically closed after a certain amount
of time of inactivity. So even if you think you're keeping a connection open
for the duration of an App - it is actually being open/close implicitly
anyway.

The best way to manage ADO is to create one ADODB.Connection object and keep
it for the life of the App. Then only open and close it as you need it.

If you can get a copy of Bill Vaughn's "ADO Examples and Best Practices",
Apress.

hth
-ralph
William Vaughn - 12 Dec 2007 18:07 GMT
And I disagree.
As I discuss at length in Chapter 9 of my book and all of my previous (11)
books, if you want to take advantage of client/server architecture and the
ability to manage server state, you need to keep the Connection open. There
are any number of powerful features that can only be implemented with a
persistent connection. Features like server-side cursors, #tempdb indexed
tables to be used in JOINs and more.

While the need to open query and close exists for ASP applications, it's not
necessary for client/server applications. Yes, this approach limits
scaling--to about 1000 users.

Signature

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

>
>> Hi all,
[quoted text clipped - 60 lines]
> hth
> -ralph
Ralph - 12 Dec 2007 19:19 GMT
> And I disagree.
> As I discuss at length in Chapter 9 of my book and all of my previous (11)
[quoted text clipped - 7 lines]
> necessary for client/server applications. Yes, this approach limits
> scaling--to about 1000 users.

Even though I highly recommend your book, IMHO you are misguided on this one
subject. (Well I also disagree with your blanket condemnation of DAO, but
that is another topic. <g>)

All the issues you are talking about take place at the Provider-Engine
level. At the App-ADO level, all an ADODB.Connection.Open/Close does is make
a request for a connection with additional attributes, or notify the pool
that a particular connection is no longer needed. I think you will find the
Close request is less deterministic than one thinks.

For the scenarios you are describing a programmer will likely not request a
connection to be closed. The ability to know when and how defines a 'Best
Practice' and is the reason I suggested your book. To keep a
ADODB.Connection object always open because one *might* be dealing with a
server-side scenario is as silly as adopting a policy to always immediately
close an open connection irrespective of the scenario. Besides the *App* is
going to 'lose' the connection anyway after any period of inactivity.

Also while, as you noted, Open/Close is important in ASP and less important
in a simple client/server, it can become important again in Laptop/Mobile
applications or apps running in a noisy/busy environment - anywhere where a
continuous connection is not guaranteed.

I like to manage my App's view of the state of the connection at the point
of attack. However, like the man said - "Horses for courses". <g>

-ralph
Paul Clement - 13 Dec 2007 15:51 GMT
¤ Hi all,
¤
¤ This question has probably been answerd before but I don't find an
¤ satisfying answer in Google.
¤
¤ What is best?
¤  Open the connection when the application loads and keep it open or
¤
¤ Open the connection just when I need it and close it immediately after the
¤ dbase operation.
¤
¤ Thanks for the advise

Personally I think it depends upon the database you are working with. With a local file based system
there probably isn't much reason to be opening and closing connections frequently. It just adds
overhead.

Most server based systems implement some sort of connection pooling and an effective use of that
would be to open and close connections only when data is required.

Paul
~~~~
Microsoft MVP (Visual Basic)
Ralph - 13 Dec 2007 17:04 GMT
> ¤ Hi all,
> ¤
[quoted text clipped - 15 lines]
> Most server based systems implement some sort of connection pooling and an effective use of that
> would be to open and close connections only when data is required.

I have no real disagreement with keeping a connection open for the duration,
since there are unlikely to be any drastic consequences one way or another.

The overhead is less than you might think since the ADO library will be
opening and closing connections implicity behind the scenes anyway.

-ralph
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.