Shlomo Shmai
Shlomo Shmai

Reputation: 81

Who is throwing (and catching) this MySQL Exception?

i'm using Python with MySQL and Django. I keep seeing this error and I can't figure out where the exception is being thrown:

Exception _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") in <bound method Cursor.__del__ of <MySQLdb.cursors.Cursor object at 0x20108150>> ignored

I have many "try" and "exception" blocks in my code--if the exception occurred within one of those, then I would see my own debugging messages. The above Exception is obviously being caught somewhere since my program does not abort when the Exception is thrown.

I'm very puzzled, can someone help me out?

Upvotes: 8

Views: 6491

Answers (5)

Alek Kowalczyk
Alek Kowalczyk

Reputation: 700

The exceptions in object destructors (__del__) are ignored, which this message indicates. If you execute some MySQL command without fetching results from the cursor (e.g. 'create procedure' or 'insert') then the exception is unnoticed until the cursor is destroyed. If you want to raise and catch an exception, call explicitly cursor.close() somewhere before going out of the scope.

Upvotes: 0

Mu Mind
Mu Mind

Reputation: 11204

I had exactly that error (using MySQLdb and Django) and discovered that the reason it was "ignored" was that it occurred in a __del__ method. Exceptions in __del__ are categorically ignored: object.__del__ datamodel

There doesn't seem to be any way to catch it from further up the stack (at least according to this thread), but you can edit MySQLdb/cursors.py or monkey-patch to get your own __del__ in there that catches the exception and drops you into a pdb prompt or logs a full traceback.

Upvotes: 8

Shlomo Shmai
Shlomo Shmai

Reputation: 93

After printing out a bunch of stuff and debugging, I figured out the problem I think. One of the libraries that I used didn't close the connection or the cursor. But this problem only shows up if I iterate through a large amount of data. The problem is also very intermittent and I still don't know who's throwing the "command out of sync" exception. But now that we closed both the connection and cursor, I don't see the errors anymore.

Upvotes: 2

mthurlin
mthurlin

Reputation: 27295

I believe this error can occur if you are using the same connection/cursor from multiple threads. However, I dont think the creators of Django has made such a mistake, but if you are doing something by yourself it can easily happen.

Upvotes: 2

Todd Moses
Todd Moses

Reputation: 11039

This is a Python Error.

See: http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/

It looks like there is a problem with your MySQLdb Query.

Upvotes: 3

Related Questions