472,958 Members | 1,455 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,958 software developers and data experts.

closing database connections

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 auto-magically when the interpreter closes.
I'd be interested to hear if any one has been bitten by not
explicitly closing database connections and under what cirumstances.
My main fear is that I will "run out of" database connections or in
some other way adversely affect server performance. I'm using recent
versions of both mysql and oracle.

Thoughts?

dustin

ps. If someone wants to try to talk me into explicitly closing files
I'd be interested to hear wisdom on that as well.
Jul 18 '05 #1
4 2539
I seem to remember reading somewhere that as client connections are a finite
(and often scarce) resource, it's not the client but the server that will
suffer when you do this.

hth,
-andyj
Jul 18 '05 #2
andy wrote:
I seem to remember reading somewhere that as client connections are a finite
(and often scarce) resource, it's not the client but the server that will
suffer when you do this.
I think there might be two issues here. One is using the reference
counting scheme to close files and databases, the other is to keep
connections open, which andy seems to be referring too.

This discussion has occured a lot in the python community. I'll give a
case in point. The python interface to metakit has no close method. It
can only be closed through reference counting and by going out of scope.

The problem with this, is that if some piece of code has a reference to
the metakit database, it will *never* be closed and this causes hard to
find bugs with metakit. In C-python reference counting closes files and
database connections "soon" after they go out of scope. This may or may
not be true in java python. I haven't tested this.

The upside is that if you explicitly close the database, it will be
closed when you expect and you don't have to worry about other objects
having a reference to your cursor or connection or what not.

That being said, just relying on the reference count and letting python
clean up has worked very well for me with metakit.
hth,
-andyj


Jul 18 '05 #3
Ken
dl**@rightnow.com (dustin lee) wrote in message news:<71**************************@posting.google. com>...
I'd be interested to hear if any one has been bitten by not
explicitly closing database connections and under what cirumstances.
My main fear is that I will "run out of" database connections or in
some other way adversely affect server performance. I'm using recent
versions of both mysql and oracle.


Yep, you should definitely and explicitely close database connections.
Failure to do so can easily leave connections open and resources
locked - not much unlike closing your ssh sessions by killing the
client rather that logging out.

Kind of a coincidence my seeing this email actually - just two minutes
ago I read an email from a team member instructing others on the team
to close their database connections (php/db2) because some connections
occasionally hang and interfere with our loads.
ken
Jul 18 '05 #4
there are two extra features to be taken into account when working
with database-connections (assuming you are working on problems that
require non-sequential DB-access):

as already mentioned DB-connections are valuable resources, they are
not only limited, their creation also is performance-relevant. so
quite soon you will find yourself working w/ connection pooling.

even more complicated are transactional problems because connections
"wrap" transactions; so there might be times when you must make sure
that consequent calls to the DB use the same transaction whereas in
other cases you need to access the DB with parallel but isolated
transactions.

So in conclusion I don't think that there is just one golden rule for
DB-connection creation and release, your golden rule will most of the
time be golden just for your actual problem.
Jul 18 '05 #5

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

Similar topics

6
by: MAB71 | last post by:
There should be a way to copy an access database ( .mdb file ) without closing connections to it. Unfortunately the FileCopy statement in VB gives an error if users are connected to the db. But I...
1
by: C Sharp beginner | last post by:
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...
2
by: Do | last post by:
Hi, Is it better to objConn = Nothing or objConn.Close. I'm starting to get an error message that
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...
7
by: Tumurbaatar S. | last post by:
Is it so important to close database connections? As I understand, after processing a request and sending a response, page object destroyed. So all used connections also destroyed. Yes?
3
by: Andy G | last post by:
I have some code that hits a DB2 database from my ASP.NET application. I haven't been able to exactly nail it down but my connections are not getting closed for some reason. I look at the...
7
by: Martien van Wanrooij | last post by:
I have been faced a couple of times with the situation that I wanted to write a script and was worried about a too frequent opening and closing mysql connections. To give some examples: 1)I...
5
by: zacks | last post by:
If I close and dispose an ODBCConnection object, shouldn't the connection actually close? I have found that even after closing and disposing an ODBCConnection, the database it was connected to...
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...
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
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
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...
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...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
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...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.