sqlalchemy下连接MYSQL出现的错误:This session is in ‘prepared‘ state; no further SQL can be emitted ...

InvalidRequestError: This session is in 'prepared' state; no further SQL can be emitted within this transaction.

在python2.7及python3下,在Flask下使用sqlalchemy session出现以上错误,以下是错误的代码:

engine = create_engine(
    db.MysqlURI,
    echo=True,
    pool_size=200,
    pool_recycle=-1,
    pool_pre_ping=True
)


DbSession = sessionmaker(bind=engine)
session = DbSession()

因为是有多个线程需要执行SQL,并且不同的SQL查询,要创建不一样的session,所以以上代码应该改成如何:

engine = create_engine(
    db.MysqlURI,
    echo=True,
    pool_size=200,
    pool_recycle=-1,
    pool_pre_ping=True
)


def NewSession():
    DbSession = sessionmaker(bind=engine)
    session = DbSession()
    return session

你可能感兴趣的:(python)