8

Using SQLAlchemy to query a MySQL database I am getting the following error:

sqlalchemy.exc.OperationalError: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely) (_mysql_exceptions.OperationalError) (1205, 'Lock wait timeout exceeded; try restarting transaction')

First, I assume that the error message comment to "consider using a session.no_autoflush block if this flush is occurring prematurely" is about the other session placing the lock, not the session I'm using for my current query? If I followed this advice would this help avoid locks on the database in general? Second, I only need to read and do not need to write modifications to the query results, so I would like to know how to ignore the lock and just read what is currently in the database. I believe the sql is NOWAIT, but I don't see how to do that in the sqlalchemy API.

7
  • Please provide the SQL statements involved (on both connections), and SHOW CREATE TABLE, and SHOW ENGINE=InnoDB STATUS; (if possible, when the problem happens). From what you have provided, we can't tell why it is locking, nor many other important details.
    – Rick James
    Jul 18, 2016 at 17:04
  • Also provide information on sqlalchemy's "no_autoflush"; we need to figure out what MySQL construct is under the covers.
    – Rick James
    Jul 18, 2016 at 17:05
  • 1
    Smells like SqlAlchemy is getting in the way.
    – Rick James
    Jul 19, 2016 at 19:14
  • 1
    To me it smells like OP is not even sure what they're doing: "First, I assume that ... is about the other session placing the lock, not the session I'm using for my current query?" What other session? As stated before: providing a minimal, complete and verifiable example would help helping. Jul 19, 2016 at 21:08
  • 1
    Or perhaps get a proper DBMS such as PostgreSQL Anyway stackoverflow.com/questions/5836623/… could be of help Jul 20, 2016 at 5:55

1 Answer 1

2

Assuming you are using the mysql.connector, the default value of the autocommit Property is False, which might cause your script to hang due to other session that is waiting to finish.

SQLAlchemy is using the BEGIN statements, (alias of START TRANSACTION) which cause the session to acquire LOCK of the table/database, and your connection will wait until the lock will get approved.

To overcome this behavior (and due to the fact that you said you only need to READ data during the session) you can set autocommit=True when creating your Session:

Session = sessionmaker(bind=engine, autocommit=True)

Another option - after you create the session you can execute SET AUTOCOMMIT=1:

s = Session()
s.execute("SET AUTOCOMMIT=0")

You can also try to set the autocommit property directly in the connection string:

engine = create_engine("mysql+mysqlconnector://user:pass@localhost/dbname?autocommit=1")

However I didn't test it. According to the documentations it should work.

3
  • 2
    One of the first things mentioned in the sqla documentation is: "Warning “autocommit” mode should not be considered for general use." Jul 19, 2016 at 21:53
  • 1
    @IljaEverilä, autocommit=True is really NOT a good practice, this is why it is mentioned this way in the documentation, HOWEVER, if there is no need for a transactional code (read-only script, for example) it will be much cheaper for the server to have that transaction with autocommit=True. The fact that SQLAlchemy have autocommit=False for every session is not always such a good practice. Moreover - If you are using the MyISAM engine it doesn't make sense to have transaction and there its even better to have autocommit=True.
    – Dekel
    Jul 22, 2016 at 20:24
  • One could say it doesn't make sense to use MyISAM. Handling a transaction can be beneficial for read-only sessions - think repeatable read. It all depends. All in all autocommit will not even help here, if and when the locking transaction is in the other session, as autocommit is just a short lived transaction. Though OP is saying that they only read, it would seem the flush before the query is issuing writes. With autocommit the flush still also occurs in a transaction. Perhaps they've modified some instance without thinking about it, but we cannot know. Jul 22, 2016 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.