]> git.ipfire.org Git - pbs.git/commitdiff
database: Only commit, but don't release sessions
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jun 2025 17:59:26 +0000 (17:59 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jun 2025 17:59:26 +0000 (17:59 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/api/uploads.py
src/buildservice/database.py

index e3c3299f14cad11cda7f07293b8366a8afde56f1..6570aa48062a02ffbac83d7380d0955bcfa5efa5 100644 (file)
@@ -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
index ecf6633d7a9e4bc31ef15ccea1d868bd27874817..c2513d14246cdcb073658b6ac0c26b1f46c47168 100644 (file)
@@ -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):
                """