Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecated Session.close_all() vs 'scoped_session' object has no attribute 'close_all_sessions'

I am trying to use 'drop_all' after service test fails or finishes on flask app layer:

@pytest.fixture(scope='class')
def db_connection():
    db_url = TestConfig.db_url
    db = SQLAlchemyORM(db_url)
    db.create_all(True)
    yield db_connection
    db.drop_all()

When some test passes the 'drop_all' works, but when it fails the test freezes.

So, that solution solves my problem: https://stackoverflow.com/a/44437760/3050042

Unfortunately, I got a mess with that.

When I use the 'Session.close_all()' SQLAlchemy warns:

The Session.close_all() method is deprecated and will be removed in a future release.  Please refer to session.close_all_sessions().

When I change to the suggestion:

AttributeError: 'scoped_session' object has no attribute 'close_all_sessions'

Yes, I use scoped_session and pure SQLAlchemy.

How to solve this?

like image 505
Avancini Avatar asked Oct 28 '25 10:10

Avancini


2 Answers

The close_all_sessions function is defined at the top level of sqlalchemy.orm.session. At the time of writing this answer, here is how it looks. Thus, you can use it as follows.

from sqlalchemy.orm.session import close_all_sessions


close_all_sessions()
like image 199
David Alber Avatar answered Oct 30 '25 15:10

David Alber


asyncio (AsyncSession) analog:

from sqlalchemy.ext.asyncio import close_all_sessions

await close_all_sessions()
like image 28
korolkevichm Avatar answered Oct 30 '25 13:10

korolkevichm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!