File tree Expand file tree Collapse file tree 3 files changed +21
-7
lines changed
Expand file tree Collapse file tree 3 files changed +21
-7
lines changed Original file line number Diff line number Diff line change @@ -110,7 +110,7 @@ from contextlib import asynccontextmanager
110110from typing import Any, AsyncGenerator
111111from fastapi import FastAPI
112112
113- from . database import master
113+ from database import master
114114
115115
116116@asynccontextmanager
@@ -195,8 +195,8 @@ from sqlalchemy import insert
195195
196196from context_async_sqlalchemy import db_session
197197
198- from .. database import master
199- from .. models import ExampleTable
198+ from database import master
199+ from models import ExampleTable
200200
201201
202202async def handler_with_db_session () -> None :
Original file line number Diff line number Diff line change 55 close_db_session ,
66 commit_db_session ,
77 db_session ,
8+ new_non_ctx_atomic_session ,
9+ new_non_ctx_session ,
810 run_in_new_ctx ,
911)
1012from sqlalchemy import insert
@@ -21,9 +23,8 @@ async def handler_multiple_sessions() -> None:
2123 await asyncio .gather (
2224 run_in_new_ctx (_insert ),
2325 run_in_new_ctx (_insert_manual ),
24- run_in_new_ctx (_insert ),
25- run_in_new_ctx (_insert_manual ),
26- run_in_new_ctx (_insert ),
26+ _insert_non_ctx (),
27+ _insert_non_ctx_manual (),
2728 )
2829
2930
@@ -41,3 +42,16 @@ async def _insert_manual() -> None:
4142
4243 # You can manually close the session if you want, but it is not necessary
4344 await close_db_session (master )
45+
46+
47+ async def _insert_non_ctx () -> None :
48+ async with new_non_ctx_atomic_session (master ) as session :
49+ stmt = insert (ExampleTable ).values (text = "example_multiple_sessions" )
50+ await session .execute (stmt )
51+
52+
53+ async def _insert_non_ctx_manual () -> None :
54+ async with new_non_ctx_session (master ) as session :
55+ stmt = insert (ExampleTable ).values (text = "example_multiple_sessions" )
56+ await session .execute (stmt )
57+ await session .commit ()
Original file line number Diff line number Diff line change @@ -98,7 +98,7 @@ async def test_example_multiple_sessions(
9898
9999 result = await db_session_test .execute (select (ExampleTable ))
100100 rows = result .scalars ().all ()
101- assert len (rows ) == 5
101+ assert len (rows ) == 4
102102 for row in rows :
103103 assert row .text == "example_multiple_sessions"
104104
You can’t perform that action at this time.
0 commit comments