Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore Lock in MYSQL Database in Sqlalchemy Query

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.

like image 1000
Michael Avatar asked Jul 14 '16 18:07

Michael


People also ask

How can remove wait timeout lock in MySQL?

Dealing With a InnoDB Lock Wait Timeout When deploying a MySQL-based cluster, ClusterControl will always set innodb_rollback_on_timeout=1 on every node. Without this option, your application has to retry the failed statement, or perform ROLLBACK explicitly to maintain the transaction atomicity.

How do I unlock a locked table in MySQL?

You can implicitly release the table locks. If the connection to the server terminates explicitly or implicitly all the locks will be released. You can release the locks of a table explicitly using the UNLOCK TABLES statement.

Does SQLAlchemy cache query?

Caching the query object so that Python interpreter doesn't have to manually re-assemble the query string every time. These queries are called baked queries and the cache is called baked . Basically it caches all the actions sqlalchemy takes BEFORE hitting the database--it does not cut down on database calls.

Can SQLAlchemy be used with MySQL?

SQLAlchemy supports MySQL starting with version 5.0. 2 through modern releases, as well as all modern versions of MariaDB. See the official MySQL documentation for detailed information about features supported in any given server release.


1 Answers

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.

like image 67
Dekel Avatar answered Sep 29 '22 02:09

Dekel