473,769 Members | 5,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Disconnected vs .. um .. connected?

I know that .NET is based on a disconnected architecture, but I can't
conceive of why continually opening and closing a connection would be
faster than leaving a connection open.
So I ran a test and came up with a result of exactly the same times!

50 rows selected into a datareader, in a loop of 50
One opens and closes the connection in each iteration of the loop, the
other just opens before the loop starts, then closes after the loop is
finished.

62.5 milliseconds for both

So why is this, and which is best? I know MS preaches disconnected, but
I need to know why, then I am converted.

Thanks

Feb 9 '06 #1
9 1823
The argument for disconnected vs connected is not over performance, it is
about scalability. If you're writing applications that require a live
connection to the DB, then you'll run into issues when the application is in
use by many users concurrently. Issues not only like physical resources on
the server, but possibly licencing issues. There are other reasons why
disconnected is a better choice as well, but I think scalability is probably
the key factor.

Sayed Ibrahim Hashimi
www.sedodream.com

"Steven Nagy" wrote:
I know that .NET is based on a disconnected architecture, but I can't
conceive of why continually opening and closing a connection would be
faster than leaving a connection open.
So I ran a test and came up with a result of exactly the same times!

50 rows selected into a datareader, in a loop of 50
One opens and closes the connection in each iteration of the loop, the
other just opens before the loop starts, then closes after the loop is
finished.

62.5 milliseconds for both

So why is this, and which is best? I know MS preaches disconnected, but
I need to know why, then I am converted.

Thanks

Feb 9 '06 #2
Ant
Open connections co$t. The fewer open, the cheaper. That's my two cents.

Ant

"Sayed Ibrahim Hashimi" wrote:
The argument for disconnected vs connected is not over performance, it is
about scalability. If you're writing applications that require a live
connection to the DB, then you'll run into issues when the application is in
use by many users concurrently. Issues not only like physical resources on
the server, but possibly licencing issues. There are other reasons why
disconnected is a better choice as well, but I think scalability is probably
the key factor.

Sayed Ibrahim Hashimi
www.sedodream.com

"Steven Nagy" wrote:
I know that .NET is based on a disconnected architecture, but I can't
conceive of why continually opening and closing a connection would be
faster than leaving a connection open.
So I ran a test and came up with a result of exactly the same times!

50 rows selected into a datareader, in a loop of 50
One opens and closes the connection in each iteration of the loop, the
other just opens before the loop starts, then closes after the loop is
finished.

62.5 milliseconds for both

So why is this, and which is best? I know MS preaches disconnected, but
I need to know why, then I am converted.

Thanks

Feb 9 '06 #3
Yeah, but WHY?

I already know that people don't like them, I need to know the why.

I just ran another test with an sql statement that utilised a simple
inner join, 2 where conditions, and an order by.
I called this sql statement with a data reader, returning 100 rows. I
called it in a loop 200 times.
I did it with the connection open all the time.
Then I did the same thing opening and closing it for every call to the
database.

Response times are practically identical.

So, WHY is this so, and WHY is it better to have fewer open
connections?
What if the environment is controlled and I can guarantee less than 5
connections?

Thanks.

Feb 9 '06 #4
Steven Nagy wrote:
I just ran another test with an sql statement that utilised a simple
inner join, 2 where conditions, and an order by.
I called this sql statement with a data reader, returning 100 rows. I
called it in a loop 200 times.
I did it with the connection open all the time.
Then I did the same thing opening and closing it for every call to the
database.


Not sure, but I think SqlConnection uses connection pooling-- it keeps
an "available" connection open (without you knowing it) and reuses it if
the conn string is the same. That's why you're seeing the same results
I suspect-- the connection is never (really) closed.

Maybe you could convince yourself (and me) by changing the connection
string (use a different user?) each time and see if that has an impact.

Scott
Feb 9 '06 #5
> Steven Nagy wrote:
I just ran another test with an sql statement that utilised a simple
inner join, 2 where conditions, and an order by.
I called this sql statement with a data reader, returning 100 rows. I
called it in a loop 200 times.
I did it with the connection open all the time.
Then I did the same thing opening and closing it for every call to the
database.


Not sure, but I think SqlConnection uses connection pooling-- it keeps an
"available" connection open (without you knowing it) and reuses it if the
conn string is the same. That's why you're seeing the same results I
suspect-- the connection is never (really) closed.

Maybe you could convince yourself (and me) by changing the connection string
(use a different user?) each time and see if that has an impact.

Scott


Yes, connection-pooling is used. For this to work, the
connections-strings should be exactly the same, not just "similar".
Switching the various arguments around will count as "different" .

The two testcases (keep a single connection open versus opening and
closing on every iteration) in reality both use just a single open
connection. That's why the results are the same.

You could also read the results the other way: if there is no
difference, why *not* close the connection as soon as you are done with
it? For a winform application I don't think there will be much
difference, for a webapplication you will *need* to close the
connection as soon as possible (as there are multiple concurrent
users).

Hans Kesting
Feb 9 '06 #6
AAJ
Hi all

The disconnected/connected question is something I've been pondering as
well. I understand the scalability and cost issues but I wonder if there is
a compromise being made elsewhere.

It struck me that one of the main benefits of client/server setups is the
data is stored safely immediately, but if apps are designed to run
disconnected, then there would be greater potential for loss of data.
Perhaps due to loss of power, crashes and other unforeseen events ( I once
accidentally stood on a multiway adapter ON/OFF switch and turned off 4
development machines), where as writing the data straight to the Server
Database means once entered, the data will be guaranteed.

Also (this isn't a dig at MS, its just a simple newbies thought), it 'seems'
much more complicated then working with the old ADO recordsets.
DataAdapters, Datasets, Datsets looking for changes, Commandbuilders , Back
to DataAdapters etc...

anyway, just my thoughts, I'm sure its been discussed long and hard by
cleverer people than me 8-)

Andy
"Hans Kesting" <ne***********@ spamgourmet.com > wrote in message
news:mn******** *************** @spamgourmet.co m...
Steven Nagy wrote:
I just ran another test with an sql statement that utilised a simple
inner join, 2 where conditions, and an order by.
I called this sql statement with a data reader, returning 100 rows. I
called it in a loop 200 times.
I did it with the connection open all the time.
Then I did the same thing opening and closing it for every call to the
database.


Not sure, but I think SqlConnection uses connection pooling-- it keeps an
"available" connection open (without you knowing it) and reuses it if the
conn string is the same. That's why you're seeing the same results I
suspect-- the connection is never (really) closed.

Maybe you could convince yourself (and me) by changing the connection
string (use a different user?) each time and see if that has an impact.

Scott


Yes, connection-pooling is used. For this to work, the connections-strings
should be exactly the same, not just "similar". Switching the various
arguments around will count as "different" .

The two testcases (keep a single connection open versus opening and
closing on every iteration) in reality both use just a single open
connection. That's why the results are the same.

You could also read the results the other way: if there is no difference,
why *not* close the connection as soon as you are done with it? For a
winform application I don't think there will be much difference, for a
webapplication you will *need* to close the connection as soon as possible
(as there are multiple concurrent users).

Hans Kesting

Feb 9 '06 #7
Thanks all.

I was not aware that that's what connection pooling was all about.

Makes a bit more sense to me now.

But my question now is this:
If connection pooling keeps an active connection anyway, whats the
point of closing my connection in code?
Is it just a matter of cleaning up my objects?

Feb 9 '06 #8
Steven Nagy wrote:
But my question now is this:
If connection pooling keeps an active connection anyway, whats the
point of closing my connection in code?
Is it just a matter of cleaning up my objects?


I just checked out Roeder's .NET Reflector for SqlConnection.C lose().
[Dispose() just calls Close()] Seems that there's a _lot_ that goes on
in close. It closes any DataReader, "unprepares " SqlCommands, and rolls
back a failed(?) transaction. ... and that's just what I got from a
cursory glance.

The moral of the story is, if a method has a "Close()" or implements
IDisposable, always call it, even if it seems unnecessary (see
MemoryStream).

Scott
Feb 10 '06 #9
> Thanks all.

I was not aware that that's what connection pooling was all about.

Makes a bit more sense to me now.

But my question now is this:
If connection pooling keeps an active connection anyway, whats the
point of closing my connection in code?
Is it just a matter of cleaning up my objects?


When you "close" the connection, you signal that it can be returned to
the pool (plus everything that Scott C said).

Hans Kesting
Feb 10 '06 #10

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

Similar topics

3
15142
by: James Yang | last post by:
Hi, I am using Sockets to connect to a remote computer and send data using Socket.Send() and receive using Socket.Receive() (block mode) . for somereason tho, when the client disconnects the Socket.Receive() just passes without any exception. Is there any way to detect disconnection using Sockets? I believe I saw a way of doing this using IAsyncResult, and eventhandling but..no article really explained how to use it properly.
6
488
by: RMG | last post by:
I use VS.net 2003 and VSS 6a. Whilst working on the network the integration between VS.net and VSS works perfectly. But when I open a project whilst disconnected from the network I get some problems. When I open a solution VS.net tells me it can't find the VSS database and gives me an option to "work disconnected". If I select this option VS.net gives me a msgbox titled "Microsoft Development Environment" with the
2
5808
by: giloosh99 | last post by:
Hello, Im grabbing tables via VB code using visual foxpro ODBC drives. The tables directory is in a mapped network drive. The code works fine and does the job, however if the computer is idle for a while the network drive apears to have a red X on the icon and the drive becomes disconnected. this seems to effect the VB code from grabbing the tables. I get an error saying the specific table cannot be found. If i manually open the mapped...
3
8776
by: light_wt | last post by:
Hi, From my understanding DataSet is used for disconnected data access in ADO.NET. However, I haven’t come across an example to use continuous data access with ADO.NET using C# . Is continuously connection possible and allowed in ADO.NET and Is there a simple example available? Thanks.
4
2320
by: Steve Le Monnier | last post by:
The ADO.NET DataSet is idea for application development, especially if you need disconnected data. DataReader objects are great in the connected environment but are forward only. What do you do when you want a connected application but need the ability scroll forward and backward within the result set. What I'm trying to achieve is to build an application that has navigation buttons, thus allowing the user to step forward or backward...
0
1074
by: Cat | last post by:
Hello. Is it POSSIBLE to get notified when the machine is connected to the internet or disconnected from the internet? I mean, using .NET class not using Windows API. Other than pinging a website continuously at a time interval. If it is impossible, please answer NO to my question. That will help me to stop searching for this. Thank you.
3
2543
by: AC | last post by:
Running VS.NET 2003 Enterprise Arch on WinXP Pro SP1 with a P4-2.4Ghz, 760MB+ RAM, and 10+GB free disk space. Laptop is part of a domain. When working on a web project connected at the office, VS.NET enters into debug mode (compiling, firing off a browser, and starting the debugger) just fine. However, when the machine is disconnected, or I'm working off a wireless network @ home (WiFi) or working off a wired connection @ home (all...
1
1948
by: dejavue82 | last post by:
Hello fellow programmers! My question concerns the scalability of database access. In coldfusion (or php for that matter) for example, there seems to be only the connected model for accessing a DB. On the other hand, there is asp.net 2.0, which provides disconnected and connected access. Question: Would databinding to controls directly, in ASP.NET 2.0 (without the manual coding of ADO.net), be as affective (scalable) as using the...
2
3334
by: Gabriel | last post by:
Hello, I'm looking for documentation with "Best Practice" for ASP.NET application In which case use Connected or Disconnected mode Typed dataset or not ? I didn'd find anything pertinent ....
0
10215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.