472,971 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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********@comcast.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*******@discussions.microsoft.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 2528
C Sharp beginner,

See inline.

"C Sharp beginner" <an*******@discussions.microsoft.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
"Implementing 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.com

I appreciate your reply.

Subject: Re: closing open connections and other resources
From: "William Ryan" <do********@comcast.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*******@discussions.microsoft.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
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...
1
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...
3
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...
1
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...
7
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...
1
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...
5
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...
1
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...
1
by: Raman Pahwa | last post by:
plz tell me how to check the number of open connections in ASP code.
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.