473,729 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

closing open connections

I'm sorry about this verbose posting. This is a follow-up
to my yesterday's posting. Thanks William for your reply.
I understand it is a good practice to open connections as
late as possible and close them as early as possible.
My requirement is as follows:
I'm developing a class library that will be instantiated
by a COM component. My class library contains functions
that perform lot of mathematical calculations and read a
lot of data (using SqlDataReader) from the SQL Server
database. Once loaded into the memory (just one instance
of my C# class), the functions are executed many times in
an hour. Also, the class instance will reside in memory
indefinitely unless some error condition occurs in one of
the other services in our applications world in which
case, this class instance will be unloaded from memory.

I'm required to open at least 3 connections (since I will
be using 3 simulataneous data readers)

My questions are:
1.Is it ok to open the connections on startup and leave
them open as long as the class instance is in memory?

2.Will it not be a performance hit to open and close
connections very often?

3.Which is the best place to close the connections and
how?

4. Can you give me a brief example for implementing
IDisposable interface?

I appreciate your reply.

Subject: Re: closing open connections and other resources
From: "William Ryan" <do********@com cast.nospam.net >
Sent: 1/5/2004 12:46:19 PM


There are a lot of good reasons not to do this. Leaving
connections open
indefinitely has many potential problems, connection
pooling being the most
noticeable.
However, you could implement IDisposable and take care of
it there.
"csharpbeginner " <an*******@disc ussions.microso ft.com>
wrote in message
news:07******** *************** *****@phx.gbl.. .
My requirement is a certain database connection should be
closed only when the object is unloaded from memory. Is
the class destructor a good place to close open database
connections? Thanks in advance for your reply.

..
Nov 15 '05 #1
1 2565
C Sharp beginner,

See inline.

"C Sharp beginner" <an*******@disc ussions.microso ft.com> wrote in message
news:09******** *************** *****@phx.gbl.. .
I'm sorry about this verbose posting. This is a follow-up
to my yesterday's posting. Thanks William for your reply.
I understand it is a good practice to open connections as
late as possible and close them as early as possible.
My requirement is as follows:
I'm developing a class library that will be instantiated
by a COM component. My class library contains functions
that perform lot of mathematical calculations and read a
lot of data (using SqlDataReader) from the SQL Server
database. Once loaded into the memory (just one instance
of my C# class), the functions are executed many times in
an hour. Also, the class instance will reside in memory
indefinitely unless some error condition occurs in one of
the other services in our applications world in which
case, this class instance will be unloaded from memory.

I'm required to open at least 3 connections (since I will
be using 3 simulataneous data readers)
Do you really need three simultaneous data readers? Are you sure that
you wouldn't be better off getting the three separate result sets one at a
time?

My questions are:
1.Is it ok to open the connections on startup and leave
them open as long as the class instance is in memory?
Absolutely not. Since you will not be using them all the time, you
should be disposing of them as soon as possible. If you keep it open, then
the connection is not able to be used in other areas (or by the server as
well), and you are just wasting resources. Open it as late as possible, and
close as soon as possible. You will get the best performance this way.

As a general rule, I do not pass database connections around to other
methods. If you do, the methods should respect the state that the
connection is in. It should open it if it is closed, and keep it open if it
is already open. When done, if it was in a closed state, then it should
close it again.

Then, in my method, I open the connection, and close when done.

2.Will it not be a performance hit to open and close
connections very often?
It will be a bigger performance hit to keep it open. Connection pooling
is most likely running in the background and will keep the connection open
if it feels that doing so would improve performance. Trust the connection
pool, it is your friend.

3.Which is the best place to close the connections and
how?
You should close the connection when you are done with it (for the
operation you are performing). Call the Close method or the Dispose method
in the IDisposable implementation.

4. Can you give me a brief example for implementing
IDisposable interface?
Check out the section of the .NET framework documentation titled
"Implementi ng a Dispose Method", located at (watch for line wrap):

http://msdn.microsoft.com/library/de...posemethod.asp

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

I appreciate your reply.

Subject: Re: closing open connections and other resources
From: "William Ryan" <do********@com cast.nospam.net >
Sent: 1/5/2004 12:46:19 PM


There are a lot of good reasons not to do this. Leaving
connections open
indefinitely has many potential problems, connection
pooling being the most
noticeable.
However, you could implement IDisposable and take care of
it there.
"csharpbeginner " <an*******@disc ussions.microso ft.com>
wrote in message
news:07******** *************** *****@phx.gbl.. .
My requirement is a certain database connection should be
closed only when the object is unloaded from memory. Is
the class destructor a good place to close open database
connections? Thanks in advance for your reply.

.

Nov 15 '05 #2

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

Similar topics

4
2590
by: dustin lee | last post by:
Over the years I've gotten out of the habit of explicitly closing file objects (whether for reading or writing) since the right thing always seems to happen auto-magically (e.g. files get written to disk with no missing data). I've recently started do the same thing with database connections. I'm wondering if anyone has had trouble with this sort of "lazy" programming style. My assumption is the the database connection will get closed...
1
2290
by: mark | r | last post by:
is there any ASP script that will display a list of all open connections on a shared host im trying to determine if my previous problem with "system resource exceeded" can be resolved by brute force? Mark www.iosilver.co.uk
3
2232
by: Anthony | last post by:
Hey all, Here's a question for you, my hosts have told me that that one my pages, php, was causing their server to reboot because there were too many open connections and that they should be closed. Now, correct me if I am wrong but I thought that the connections were closed after a minute of inactivity. Does this sound right? Could open connections cause their server to reboot a
1
1304
by: csharpbeginner | last post by:
My requirement is a certain database connection should be closed only when the object is unloaded from memory. Is the class destructor a good place to close open database connections? Thanks in advance for your reply.
7
1410
by: darrel | last post by:
We're running into a problem on our new site. Once a week or so, our site goes down with an 'out of memory error'. Rebooting the web server fixes things. Googling the error doesn't return many results (one, actually) and the suggested fix is to make sure you are manually closing all DB connections. I'm doing that now, but I had thought that asp.net had fairly robust automated cleanup, and that having to explicitely close every connection...
1
2559
by: David Teran | last post by:
Hi, is it possible (i am pretty sure) to list the open connections clients have to a particular database and to force close the connection? regards David ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
5
8538
by: TimSki | last post by:
Hi, We are using a remotely hosted sql serer 2005 db. We connect to it/run queries etc. using sql server management studio express. Is there anyway to view the number of open connections using ssmse, in particular we want to see the number that are being left open ? Thanks in advance
1
3042
by: lee2732 | last post by:
I am running a website at GoDaddy where I have their least expensive plan that supports Linux/ColdFusion/mySQL. Apparently this plan supports a small number of active database connections (50). I wrote my ColdFusion code in Dreamweaver and just assumed it would take care of issues like this, but I guess it doesn't. If I have a few windows open on the same machine and just keep hitting the site, I can easily cause it to 'crash' to the point...
1
2622
by: Raman Pahwa | last post by:
plz tell me how to check the number of open connections in ASP code.
0
8763
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
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...
0
9288
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9154
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
8156
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
6722
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...
1
3240
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
2
2692
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2166
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.