From: Michael Tremer Date: Sun, 29 Jun 2025 17:59:26 +0000 (+0000) Subject: database: Only commit, but don't release sessions X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=01088e742635b5bebeb611fb64e9f750ccf55489;p=pbs.git database: Only commit, but don't release sessions Signed-off-by: Michael Tremer --- diff --git a/src/api/uploads.py b/src/api/uploads.py index e3c3299f..6570aa48 100644 --- a/src/api/uploads.py +++ b/src/api/uploads.py @@ -79,7 +79,7 @@ async def create(request: UploadRequest, current_principal = fastapi.Depends(auth.get_current_principal)) -> uploads.Upload: # Create a new upload try: - async with backend.db: + async with backend.db as session: upload = await backend.uploads.create( filename = request.filename, size = request.size, @@ -113,7 +113,7 @@ async def put(request: fastapi.Request, upload: uploads.Upload = fastapi.Depends # Stream the payload try: - async with backend.db: + async with backend.db as session: async with upload.stream() as f: async for chunk in request.stream(): await f.write(chunk) @@ -129,7 +129,7 @@ async def put(request: fastapi.Request, upload: uploads.Upload = fastapi.Depends @router.delete("/{id}") async def delete(upload: uploads.Upload = fastapi.Depends(get_upload)): # Delete the upload - async with backend.db(): + async with backend.db() as session: await upload.delete() # Send 204 diff --git a/src/buildservice/database.py b/src/buildservice/database.py index ecf6633d..c2513d14 100644 --- a/src/buildservice/database.py +++ b/src/buildservice/database.py @@ -204,7 +204,7 @@ class Connection(object): # # It can be used as follows: # - # async with backend.db: + # async with backend.db as session: # ... async def __aenter__(self): @@ -214,12 +214,10 @@ class Connection(object): return await self.session() async def __aexit__(self, type, exception, traceback): - # This method will be called when the block is being excited and it will - # release the database session. Usually that means that there will be a commit. - task = asyncio.current_task() - - # Immediately release the session - await self.__release_session(task) + # If we leave the block, we automatically commit, but we keep the session + # alive so that any objects that we are using won't expire and require refresh. + if exception is None: + await self.commit() async def session(self): """