473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

When to open a database connection?

I read in a Visual Studio .NET book that with C# it is now recommended to
open database connections whenever you need to query a database as opposed
to the traditional method of opening a database at the start of your app and
closing it at the end. Their reasoning was that "opening a database
connection is no longer an expensive operation".

Do you guys agree?
Nov 16 '05 #1
6 2888
Keith,

It has simple to do with connection pooling in SQL server.
Where leaving the connections open is expensive.

When you use by instance MS access you can think about leaving it open
because otherwise somebody can replace the accessfile because it is not
locked and than you get a strange situation.

Because of that connectionpooling is as well advised as an exception on the
normal use of close, to use connection.dispose instead of connection.close
in the versions 2002/2003. That last will be gone in the version 2005.

Cor
Nov 16 '05 #2
> It has simple to do with connection pooling in SQL server.
Where leaving the connections open is expensive.

When you use by instance MS access you can think about leaving it open
because otherwise somebody can replace the accessfile because it is not
locked and than you get a strange situation.

Because of that connectionpooling is as well advised as an exception on
the normal use of close, to use connection.dispose instead of
connection.close in the versions 2002/2003. That last will be gone in the
version 2005.

Cor


Thanks alot!

My database is an MS Access Database. Is there anything I need to do to
make it "multi-user" capable? Or is this already how it is set up by
default?
Nov 16 '05 #3
> My database is an MS Access Database. Is there anything I need to do to
make it "multi-user" capable? Or is this already how it is set up by
default?


AFAIK, it is multiuser by default, you need to request exclusivity
explicitly to make it single-user with an attribute in the connection
string.

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com

Nov 16 '05 #4
Keith Smith wrote:
I read in a Visual Studio .NET book that with C# it is now recommended to
open database connections whenever you need to query a database as opposed
to the traditional method of opening a database at the start of your app and
closing it at the end.


I recently profiled the expense of creating and opening connections on
the fly using intel vTune. One method I profiled looked sort of like this:

DataSet Load(int x)
{
using(IDbConnection con = Factory.CreateConnection())
{
conn.Open();
DataSet result = new DataSet();

IDbDataAdapter adapter1, adapter2;
// initialize adapters and sql text using x and conn
adapter1.Fill(result);
adapter2.Fill(result);
return result;
}
}

Everytime a certain form opened, this routine was called about 200 times
(in a background thread, FWIW). The execution profile of Load broke down
like this:

total time spent inside this method: 7.397 s
execution count : 273
adapter1.Fill -> 3.814 s == 51.6%
adapter2.Fill -> 2.714 s == 36.7%
conn.Open -> 0.841 s == 11.4%

avg time to open connection : 3.0 ms

Both queries utilized an index on the table. Each datatable had on
average about 50 rows and 6 columns.

Based on the results that I saw, I agree with the "Just create
connections on the fly" philosophy, but don't create them over and over
again in a loop, if you can avoid it.

Creating them on the fly can also make life easier in other ways. First,
it makes it easier to write a using statement to dispose the connection.
Also, if you are writing a multithreaded app, you won't make the mistake
of using the same connection in two threads at the same time. My Load
method can be called from multiple threads without worrying about
synchronization issues.

H^2

Nov 16 '05 #5
the recommendation is only for windows forms applications.

ideally, you would want to turn connection pooling on if it isn't so that
connections are recycled instead of the expensive create step

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------
"Harold Howe" <hh***@gowebway.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
Keith Smith wrote:
I read in a Visual Studio .NET book that with C# it is now recommended to
open database connections whenever you need to query a database as
opposed to the traditional method of opening a database at the start of
your app and closing it at the end.


I recently profiled the expense of creating and opening connections on the
fly using intel vTune. One method I profiled looked sort of like this:

DataSet Load(int x)
{
using(IDbConnection con = Factory.CreateConnection())
{
conn.Open();
DataSet result = new DataSet();

IDbDataAdapter adapter1, adapter2;
// initialize adapters and sql text using x and conn
adapter1.Fill(result);
adapter2.Fill(result);
return result;
}
}

Everytime a certain form opened, this routine was called about 200 times
(in a background thread, FWIW). The execution profile of Load broke down
like this:

total time spent inside this method: 7.397 s
execution count : 273
adapter1.Fill -> 3.814 s == 51.6%
adapter2.Fill -> 2.714 s == 36.7%
conn.Open -> 0.841 s == 11.4%

avg time to open connection : 3.0 ms

Both queries utilized an index on the table. Each datatable had on average
about 50 rows and 6 columns.

Based on the results that I saw, I agree with the "Just create connections
on the fly" philosophy, but don't create them over and over again in a
loop, if you can avoid it.

Creating them on the fly can also make life easier in other ways. First,
it makes it easier to write a using statement to dispose the connection.
Also, if you are writing a multithreaded app, you won't make the mistake
of using the same connection in two threads at the same time. My Load
method can be called from multiple threads without worrying about
synchronization issues.

H^2

Nov 16 '05 #6
Alvin Bruney [MVP] wrote:
the recommendation is only for windows forms applications.

ideally, you would want to turn connection pooling on if it isn't so that
connections are recycled instead of the expensive create step


I believe pooling was on in my test. Otherwise, the open call would have
taken longer, I think.

PS: I only write windows forms apps.

H^2

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jerry Hull | last post by:
I'm working with a database developed by an untrained person over several years - and on a network that has recently been upgraded with a new server installed and MS office upgraded from 2K (I...
7
by: Dan Bass | last post by:
In a somewhat complex application, I've developed plug-in architecture and am having a problem as to when to catch general exceptions for logging purposes. In each plug-in class library, for...
1
by: Brian | last post by:
It's still not working - is still looking for the Database in the same folder as the photo just added. But i've realised something... bear with me here! :) The basics of the way my photo album...
4
by: Macca | last post by:
Hi, I have an windows forms application that accesses a SQL database I have a few questions as to connecting to the database. This application will run 24 hours a day. It is a monitoring...
13
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error...
6
by: Brian Henry | last post by:
is there a way to tell when the database connection is retrieveing data? i saw the connectionstate.fetching enumerated value, but of course it's not implemented yet... is there another way to do...
0
by: gm | last post by:
Immediately after generating the Access application from the Source Safe project I get: "-2147467259 Could not use ''; file already in use." If Access database closed and then reopened I get:...
20
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I...
2
by: =?Utf-8?B?U3VuaWwgUGFuZGl0YQ==?= | last post by:
Hi All, I have created a VB.Net application that used classic ADO to access the oracle database. I have mentained a persistant connection in it. When i start the application and it works fine....
2
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.