MySQL : Too many connections

Steve Holden sholden at holdenweb.com
Sun Mar 16 20:49:39 EST 2003


"Thomas Weholt" <2002 at weholt.org> wrote in message
news:574ca.38057$Rc7.543778 at news2.e.nsc.no...
> It's a application that scans a cdrom/dvd-rom and inserts records
describing
> each file found. Then I create an index of words used in filename and
paths
> on the media that was scanned. This process contains both inserts and
> selects on allready inserted records. It's a script run once a while, not
a
> long-running process.
>
> It seems as if the opened connections are not closed after the script has
> finished. Why? Do I have to call close on the connections manually? This
is
> not done in any of the examples I've seen. In "MySQL Control Center" I see
> more and more open connections listed as I run my code.
>
> Thomas
>
> "Jeff Bauer" <jbauer at rubic.com> wrote in message
> news:mailman.1047569967.24366.python-list at python.org...
> > Thomas,
> >
> > You haven't indicated what type of application you're
> > running with MySQL.  For web applications, I create a
> > single database connection object per user session,
> > running as a persistent connection (a.k.a long running
> > process).  All database queries are passed through the
> > database connection object.
> >
> > In other words, you should only require one database
> > connection per process space (or modulo # of databases,
> > if accessing multiple databases).
> >
> > Jeff Bauer
> > Rubicon Research
> >
> >
> > > I get an exception using MySQL, "Too many connections". I understand
> > > the error and why it occurrs, but how can I avoid it?? Do I close
> > > after each cursor.execute-call or do I close the connection?? In
> > > case of closing after the execute-call, how ??
> > >
> > > What is the proper way of inserting a lot of records and during
> > > this process check rows allready inserted and avoid the problem
> > > above?
> >

As Jeff Bauer says, it doesn't seem to make sense that you would be using
more than a few connections for the functionality you describe.

Once you have opened a connection, you can create as many cursors as you
like to use that connection. Each cursor is capable of executing many
queries. The only way you would increase the number of open connections show
by MySQL is to repeatedly call MySQLdb.connect(), without closing
previously-opened connections.

While closing previous connections *will* avoid the problem, so will not
opening the connections in the first place.

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Register for PyCon now!            http://www.python.org/pycon/reg.html







More information about the Python-list mailing list