esnme / ultramysql

A fast MySQL driver written in pure C/C++ for Python. Compatible with gevent through monkey patching.
http://www.esn.me
Other
547 stars 95 forks source link

query('SELECT...) returns tuple (0L, 0L) instead of rows/fields, sometimes #59

Open denis-ryzhkov opened 9 years ago

denis-ryzhkov commented 9 years ago

Versions: umysql-2.61, python-2.7.6, gevent-1.0.1.

query('SELECT...) returns tuple (0L, 0L) sometimes, as if it is query('INSERT...) or query('UPDATE...).

In almost all cases exactly the same query returns expected "result.rows" and "result.fields". But then it suddenly returns tuple (0L, 0L) and this behaviour can be cured by reconnecting to DB. I catch this bug several times a day, so I may add any debug code - please advise.

Current workaround:

    result = db_conn.query(sql, values)

    if sql.lstrip().startswith('SELECT') and isinstance(result, tuple):
        log.error('reconnecting to db on tuple SELECT: {}'.format(result)) # Logs: (0L, 0L)
        try:
            db_conn.close()
        except Exception:
            pass
        db_conn = umysql.Connection()
        db_conn.connect(...)
        return db_conn.query(sql, values) # Normal "result.rows" this time.

    return result