Problem of 'fetchone'

Issue #3748 closed
Jialin Tian created an issue

Hi SQLAlchemy Team,

Thanks for creating this awesome library.

Not sure of if it is bug. Maybe I'm using it in wrong way. Please help to see this issue.

I have been using MySQL-Python for a long time. Recently I tried to integrated a connection pool which is based on SQLAlchemy, In terms of the legacy code, I'm using the raw_connection from the engine.

Here is the sample code of two implementations:

No Pool Version:

connection = MySQLdb.connect(...)

connection.autocommit(True)
try:
    cursor = db.cursor()
    if not cursor.execute(sql, values) > 0:
            return None
    row = cursor.fetchone()
finally:
    connection.close()
return row[0]

Pool Version:

pool = create_engine("mysql+mysqldb://...")
connection = pool.raw_connection()

connection.autocommit(True)
try:
    cursor = db.cursor()
    if not cursor.execute(sql, values) > 0:
            return None
    row = cursor.fetchone()
finally:
    connection.close()
return row[0]

The codes look similar except the way to obtain the connection. After using the pool version, sometimes(not every time, actually in my situation, it occurs with 0.01% of all db queries), the return value of execute method is great than 0 and the fetchone method will return None. I guess it may related to the connection reuse, but I have no idea of which part is going wrong. Does anyone can help me?

Comments (2)

  1. Log in to comment