66import sqlalchemy as sa
77import sqlalchemy .orm as sa_orm
88from ellar .app import current_injector
9- from ellar .threading import execute_coroutine_with_sync_worker
9+ from ellar .threading import run_as_async
1010from sqlalchemy .ext .asyncio import AsyncSession
1111
1212from ellar_sql .model .base import ModelBase
@@ -275,7 +275,13 @@ def __init__(
275275 )
276276
277277 if self ._created_session :
278- self ._session .close () # session usage is done but only if Paginator created the session
278+ self ._close_session () # session usage is done but only if Paginator created the session
279+
280+ @run_as_async
281+ async def _close_session (self ) -> None :
282+ res = self ._session .close ()
283+ if isinstance (res , t .Coroutine ):
284+ await res
279285
280286 def _get_session (self ) -> t .Union [sa_orm .Session , AsyncSession , t .Any ]:
281287 self ._created_session = True
@@ -284,14 +290,15 @@ def _get_session(self) -> t.Union[sa_orm.Session, AsyncSession, t.Any]:
284290
285291 def _query_items (self ) -> t .List [t .Any ]:
286292 if self ._is_async :
287- res = execute_coroutine_with_sync_worker ( self ._query_items_async () )
293+ res = self ._query_items_async ()
288294 return list (res )
289295 return self ._query_items_sync ()
290296
291297 def _query_items_sync (self ) -> t .List [t .Any ]:
292298 select = self ._select .limit (self .per_page ).offset (self ._query_offset )
293299 return list (self ._session .execute (select ).unique ().scalars ())
294300
301+ @run_as_async
295302 async def _query_items_async (self ) -> t .List [t .Any ]:
296303 session = t .cast (AsyncSession , self ._session )
297304
@@ -302,7 +309,7 @@ async def _query_items_async(self) -> t.List[t.Any]:
302309
303310 def _query_count (self ) -> int :
304311 if self ._is_async :
305- res = execute_coroutine_with_sync_worker ( self ._query_count_async () )
312+ res = self ._query_count_async ()
306313 return int (res )
307314 return self ._query_count_sync ()
308315
@@ -313,6 +320,7 @@ def _query_count_sync(self) -> int:
313320 ).scalar ()
314321 return out # type:ignore[return-value]
315322
323+ @run_as_async
316324 async def _query_count_async (self ) -> int :
317325 session = t .cast (AsyncSession , self ._session )
318326
0 commit comments