[mod_python] 2006 "MySQL server has gone away" failures

Martijn Moeling martijn at xs4us.nu
Fri Jan 12 11:30:38 EST 2007


Hi

First you should create a database connection non globally the best way
to do so is to create it as part of the req object in your handler

I suggest you do in your handler routine:

def handler(req):
	req.database=whatever to connect to your database
	req.cursor=req.database.cursor()
	req.register_cleanup(closedb,req)	
	... # dor the rest of your stuff

Note the register cleanup line, this is always called, even  if the
request fails with a python error:

def closedb(req):
	req.cursor.close()
	req.database.close()
	


Now the connections get closed nicely and mysql does not run out of
resources anymore.

Martijn

-----Oorspronkelijk bericht-----
Van: mod_python-bounces at modpython.org
[mailto:mod_python-bounces at modpython.org] Namens Fred of UrlBit.Us
Verzonden: Friday, January 12, 2007 4:45 PM
Aan: mod_python at modpython.org
Onderwerp: [mod_python] 2006 "MySQL server has gone away" failures

Hello, all.

I intend to use mod_python for a couple of major websites, but I am
running 
into an annoying problem.

I've seen something similar posted elsewhere, but no firm resolution.

My mod_python server will run just fine for a while following a restart
of 
Apache, but after some time I get the following OperationalError
exception:

OperationalError: (2006, 'MySQL server has gone away')

I can't seem to nail this one down, or even why it would happen. I even
tried 
pinging the connection early on in the handler, but still no dice.

A restart of the server eliminates the problem for a while. Also,
sometimes 
the problem goes away on its own (but comes back later). And of course,
when 
I am sitting there trying to reproduce the problem, it works perfectly!

Any clues or suggestions would be extremely helpful.

Setup: Apache 2, Python 2.5, mod_python 3.2.10

stack dump:

ERROR In PyClops plugin <module 'app.urlbit' 
from '/home/urlbit/site/web/php/app/urlbit/__init__.py'> 
            Traceback (most recent call last):
  File "/home/ecms/py/pyclops/plugins/__init__.py", line 34, in 
_application_dispatcher
    if plug.handle(pyreq):
  File "/home/urlbit/site/web/php/app/urlbit/__init__.py", line 52, in
handle
    psp.handle(pyreq)
  File "/home/ecms/py/pyclops/psp.py", line 25, in handle
    p.run()
  File "/usr/local/lib/python2.5/site-packages/mod_python/psp.py", line
213, 
in run
    exec code in global_scope
  File "/home/urlbit/site/web/create.html", line 100, in <module>
  File "/usr/local/lib/python2.5/site-packages/MySQLdb/cursors.py", line
163, 
in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python2.5/site-packages/MySQLdb/connections.py",
line 
35, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (2006, 'MySQL server has gone away')

-- Fred
http://urlbit.us
Bite those long URLs!
_______________________________________________
Mod_python mailing list
Mod_python at modpython.org
http://mailman.modpython.org/mailman/listinfo/mod_python



More information about the Mod_python mailing list