from .result import AsyncResult
from .result import AsyncScalarResult
from ... import util
-from ...orm import close_all_sessions
+from ...orm import close_all_sessions as _sync_close_all_sessions
from ...orm import object_session
from ...orm import Session
from ...orm import SessionTransaction
"2.0",
"The :meth:`.AsyncSession.close_all` method is deprecated and will be "
"removed in a future release. Please refer to "
- ":func:`.session.close_all_sessions`.",
+ ":func:`_asyncio.close_all_sessions`.",
)
async def close_all(cls) -> None:
"""Close all :class:`_asyncio.AsyncSession` sessions."""
- await greenlet_spawn(close_all_sessions)
+ await close_all_sessions()
async def __aenter__(self: _AS) -> _AS:
return self
return AsyncSession._retrieve_proxy_for_target(session, regenerate=False)
+async def close_all_sessions() -> None:
+ """Close all :class:`_asyncio.AsyncSession` sessions.
+
+ .. versionadded:: 2.0.23
+
+ .. seealso::
+
+ :func:`.session.close_all_sessions`
+
+ """
+ await greenlet_spawn(_sync_close_all_sessions)
+
+
_instance_state._async_provider = async_session # type: ignore
from sqlalchemy.ext.asyncio import async_sessionmaker
from sqlalchemy.ext.asyncio import AsyncAttrs
from sqlalchemy.ext.asyncio import AsyncSession
+from sqlalchemy.ext.asyncio import close_all_sessions
from sqlalchemy.ext.asyncio import exc as async_exc
from sqlalchemy.ext.asyncio.base import ReversibleProxy
from sqlalchemy.orm import DeclarativeBase
)
@async_test
- async def test_close_all(self, async_session):
- User = self.classes.User
- u = User(name="u")
- async_session.add(u)
- await async_session.commit()
+ async def test_close_all(self, async_engine):
+ users, User = self.tables.users, self.classes.User
+
+ self.mapper_registry.map_imperatively(User, users)
+
+ s1 = AsyncSession(async_engine)
+ u1 = User()
+ s1.add(u1)
+
+ s2 = AsyncSession(async_engine)
+ u2 = User()
+ s2.add(u2)
+
+ assert u1 in s1
+ assert u2 in s2
+
+ await close_all_sessions()
+
+ assert u1 not in s1
+ assert u2 not in s2
+
+ @async_test
+ async def test_session_close_all_deprecated(self, async_engine):
+ users, User = self.tables.users, self.classes.User
+
+ self.mapper_registry.map_imperatively(User, users)
+
+ s1 = AsyncSession(async_engine)
+ u1 = User()
+ s1.add(u1)
+
+ s2 = AsyncSession(async_engine)
+ u2 = User()
+ s2.add(u2)
+
+ assert u1 in s1
+ assert u2 in s2
+
with expect_deprecated(
r"The AsyncSession.close_all\(\) method is deprecated and will "
"be removed in a future release. "
):
await AsyncSession.close_all()
- assert async_session.sync_session.identity_map.values() == []
+
+ assert u1 not in s1
+ assert u2 not in s2
class AsyncSessionQueryTest(AsyncFixture):