13

The code is a step-by-step copy from sqlahcmey's orm tutorial, except the last line, I intended to drop all tables after the query. But the program blocked on Base.metadata.drop_all(bind=engine), below is the status of MySQL at that time(taken from MySQL Workbench):

workbench admin

As the marked line shows, the drop table process was hanged due to the table metadata lock, I suggest the metadata lock was caused by result = session.query(User).all() since the program did not block if that line was removed, but I still don't know the reason. So my question is: Why did this happen, how to avoid the blocking

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String


Base = declarative_base()


class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String(16))
    fullname = Column(String(16))
    password = Column(String(16))

    def __init__(self, name, fullname, password):
        self.name = name
        self.fullname = fullname
        self.password = password

    def __repr__(self):
        return "<User('%s','%s', '%s')>" % (self.name, self.fullname, self.password)


uri = 'mysql://root:zxsaqw21@localhost/test_sa'
engine = create_engine(uri, echo=False)

Base.metadata.create_all(engine)

Session = sessionmaker(bind=engine)
session = Session()

user = User('ed', 'Ed Jones', 'edspassword')
session.add(user)
session.commit()

result = session.query(User).all()
print len(result)

Base.metadata.drop_all(bind=engine)
Reorx
  • 2,801
  • 2
  • 24
  • 29

1 Answers1

22

call session.close() (or commit(), or rollback()) before you do the drop_all(). the session is still sitting on an open transaction.

the tutorial is against sqlite which doesn't have aggressive table locking (I'm assuming your MySQL DB is using InnoDB here).

zzzeek
  • 72,307
  • 23
  • 193
  • 185