sqlalchemy, postgresql and relationship stuck in “idle in transaction”

后端 未结 2 673
梦如初夏
梦如初夏 2021-02-05 22:06

I have a problem related to sqlalchemy and postgresql.

class Profile(Base):
  ...

  roles = relationship(\'Role\', secondary=role_profiles,
                             


        
2条回答
  •  盖世英雄少女心
    2021-02-05 22:49

    Starting with SQLAlchemy 0.8.2 you can disable the implicit BEGIN statements when calling create_engine()

    engine = create_engine(uri, isolation_level="AUTOCOMMIT")
    

    There are some subtle implications to this change. First, there statements that were not quietly hid in unterminated transaction will be quietly ignored

    session.execute("DELETE FROM department WHERE department_id=18")
    sys.exit(0)
    

    default:

    LOG:  statement: BEGIN
    LOG:  statement: show standard_conforming_strings
    LOG:  statement: DELETE FROM department WHERE department_id=18
    LOG:  unexpected EOF on client connection with an open transaction
    

    autocommit:

    LOG:  statement: show standard_conforming_strings
    LOG:  statement: DELETE FROM department WHERE department_id=18
    

    Second, updating multiple updates are no longer automic, and rollback() is only conditionally effective:

    department = Department(u"HR")
    session.add(department)
    session.flush()
    employee = Employee(department.department_id, u'Bob')
    session.add(employee)
    session.rollback()
    

    default:

    LOG:  statement: BEGIN
    LOG:  statement: INSERT INTO department (name) VALUES ('HR') RETURNING department.department_id
    LOG:  statement: ROLLBACK
    

    autocommit:

    LOG:  statement: INSERT INTO department (name) VALUES ('HR') RETURNING department.department_id
    

    Setting SQLAlchemy's isolation_level on the Engine object is effective for many applications. It unfortunate that Session.begin() does not always mean BEGIN TRANSACTION;

提交回复
热议问题