@@ -174,11 +174,13 @@ async def scalar(
174174 Usage and parameters are the same as that of :meth:`_orm.Session.execute`;
175175 the return result is a scalar Python value.
176176 """
177+ need_close = False
177178 if session is None or not isinstance (session , AsyncSession ):
179+ need_close = True
178180 executor = self .session_maker ()
179181 else :
180182 executor = session
181- async with ExecutorContextManager (executor , need_close = session is None ) as executor :
183+ async with ExecutorContextManager (executor , need_close = need_close ) as executor :
182184 result = await executor .scalar (
183185 statement ,
184186 params ,
@@ -203,11 +205,13 @@ async def scalars_all(
203205 Usage and parameters are the same as that of :meth:`_orm.Session.execute`;
204206 the return result is a list of scalar Python value.
205207 """
208+ need_close = False
206209 if session is None or not isinstance (session , AsyncSession ):
210+ need_close = True
207211 executor = self .session_maker ()
208212 else :
209213 executor = session
210- async with ExecutorContextManager (executor , need_close = session is None ) as executor :
214+ async with ExecutorContextManager (executor , need_close = need_close ) as executor :
211215 result = (await executor .scalars (
212216 statement ,
213217 params ,
@@ -268,11 +272,13 @@ async def get(
268272 )
269273 ```
270274 """
275+ need_close = False
271276 if session is None or not isinstance (session , AsyncSession ):
277+ need_close = True
272278 executor = self .session_maker ()
273279 else :
274280 executor = session
275- async with ExecutorContextManager (executor , need_close = session is None ) as executor :
281+ async with ExecutorContextManager (executor , need_close = need_close ) as executor :
276282 result = await executor .get (
277283 entity ,
278284 ident ,
@@ -302,11 +308,13 @@ async def save(
302308 Args:
303309 session: If not specified, an `AsyncSession` is created.
304310 """
311+ need_close = False
305312 if session is None or not isinstance (session , AsyncSession ):
313+ need_close = True
306314 executor = self .session_maker ()
307315 else :
308316 executor = session
309- async with ExecutorContextManager (executor , need_close = session is None ) as executor :
317+ async with ExecutorContextManager (executor , need_close = need_close ) as executor :
310318 executor .add_all (instances )
311319 await executor .commit ()
312320 if refresh :
@@ -428,11 +436,13 @@ def scalar(
428436 session : Optional [Session ] = None ,
429437 ** kw : Any ,
430438 ) -> Any :
439+ need_close = False
431440 if session is None or not isinstance (session , Session ):
441+ need_close = True
432442 executor = self .session_maker ()
433443 else :
434444 executor = session
435- with ExecutorContextManager (executor , need_close = session is None ) as executor :
445+ with ExecutorContextManager (executor , need_close = need_close ) as executor :
436446 result = executor .scalar (
437447 statement ,
438448 params ,
@@ -452,11 +462,13 @@ def scalars_all(
452462 session : Optional [Session ] = None ,
453463 ** kw : Any ,
454464 ) -> List [Any ]:
465+ need_close = False
455466 if session is None or not isinstance (session , Session ):
467+ need_close = True
456468 executor = self .session_maker ()
457469 else :
458470 executor = session
459- with ExecutorContextManager (executor , need_close = session is None ) as executor :
471+ with ExecutorContextManager (executor , need_close = need_close ) as executor :
460472 result = executor .scalars (
461473 statement ,
462474 params ,
@@ -478,11 +490,13 @@ def get(
478490 execution_options : Optional [_ExecuteOptions ] = None ,
479491 session : Optional [Session ] = None ,
480492 ) -> Optional [_T ]:
493+ need_close = False
481494 if session is None or not isinstance (session , Session ):
495+ need_close = True
482496 executor = self .session_maker ()
483497 else :
484498 executor = session
485- with ExecutorContextManager (executor , need_close = session is None ) as executor :
499+ with ExecutorContextManager (executor , need_close = need_close ) as executor :
486500 result = executor .get (
487501 entity ,
488502 ident ,
@@ -504,11 +518,13 @@ def save(
504518 refresh : bool = False ,
505519 session : Optional [Session ] = None
506520 ) -> None :
521+ need_close = False
507522 if session is None or not isinstance (session , Session ):
523+ need_close = True
508524 executor = self .session_maker ()
509525 else :
510526 executor = session
511- with ExecutorContextManager (executor , need_close = session is None ) as executor :
527+ with ExecutorContextManager (executor , need_close = need_close ) as executor :
512528 executor .add_all (instances )
513529 executor .commit ()
514530 if refresh :
0 commit comments