Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When a Flask application with Flask-SQLAlchemy is running, how do I use MySQL client on the same database at the same time?

When I run a Flask application which is using Flask-SQLAlchemy, it seems that Flask-SQLAlchemy is holding a session and when I issue a MySQL command, like alter table add column, in MySQL client terminal, the commands cannot be executed until I quit the Flask application.

Does anybody have similar experience? How can I issue commands on MySQL client without interrupting the Flask application?

like image 486
hllau Avatar asked Sep 12 '12 08:09

hllau


1 Answers

You may want to look at this question SQL Alchemy Relationship loader leaves a lock on table?

What you'll need to do is subclass flask.ext.sqlalchemy.SQLAlchemy and override the apply_driver_hacks method to pass through the additional keyword argument isolation_level='READ <some level>':

from flask.ext.sqlalchemy import SQLAlchemy

class UnLockedAlchemy(SQLAlchemy):
    def apply_driver_hacks(self, app, info, options):
        if not "isolation_level" in options:
            options["isolation_level"] = "READ COMMITTED"  # For example
        return super(UnLockedAlchemy, self).apply_driver_hacks(app, info, options)
like image 134
Sean Vieira Avatar answered Sep 23 '22 00:09

Sean Vieira