473,661 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2587
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.c om (dustin lee) wrote in message news:<71******* *************** ****@posting.go ogle.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
11410
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 can copy the file using windows explorer so there must be a way. WinAPI function or something? I'm using VB6 and Access 2000
1
2562
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 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...
2
1219
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
1399
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...
7
1797
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
2660
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 database and it's like everytime I click a button in the ASP.NET applicaiton and it runs a database command it leaves the connection open. The DBA's are not happy with me right now. Luckily I'm still in test. Does anyone have any idea why the below...
7
3248
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 am trying to develop a photoalbum (also posted a topic called "array / mysql question"). Of course I can use some wonderful open source albums but at the same time I want to get more familiar with all the ins and outs so my idea is a list of...
5
2358
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 still has an active connection as long as the application is still running. I have to close the application to actually close the active connection to the database. Is this normal behaviour? Is there a way to really close the connection while the...
1
3034
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...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
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,...
1
8545
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
7365
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
6185
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
4179
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
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
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.