Skip to content

Commit

Permalink
get session from pool
Browse files Browse the repository at this point in the history
  • Loading branch information
PSNAppz committed May 14, 2024
1 parent 0a667f7 commit d5bd262
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/openg2p_spar_mapper_api/services/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
UpdateStatusReasonCode,
)
from sqlalchemy import delete, select
from sqlalchemy.ext.asyncio import async_sessionmaker
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession

from ..config import Settings
from ..models import IdFaMapping
Expand Down Expand Up @@ -206,7 +206,7 @@ def construct_single_update_response_for_failure(

async def resolve(self, resolve_request: ResolveRequest):
session_init = SessionInitializer.get_component()
session = await session_init.get_session_from_pool()
session: AsyncSession = await session_init.get_session_from_pool()
async with session.begin():
resolve_request_message: ResolveRequestMessage = resolve_request.message

Expand Down
18 changes: 10 additions & 8 deletions src/openg2p_spar_mapper_api/services/session_service.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from openg2p_fastapi_common.context import dbengine
from openg2p_fastapi_common.service import BaseService
from sqlalchemy.ext.asyncio import async_sessionmaker
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession


class SessionInitializer(BaseService):
def __init__(self):
super().__init__("SessionInitializer")
self.session = None
self.session_maker = None

async def get_session_from_pool(self):
if not self.session:
session_maker = async_sessionmaker(dbengine.get(), expire_on_commit=False)
async with session_maker() as session:
self.session = session
return self.session
async def get_session_from_pool(self) -> AsyncSession:
if not self.session_maker:
self.session_maker = async_sessionmaker(
dbengine.get(), expire_on_commit=False
)

async with self.session_maker() as session:
return session

0 comments on commit d5bd262

Please sign in to comment.