1212 Dict )
1313
1414import aiopg
15- from psycopg2 .extras import register_uuid , RealDictRow
15+ from psycopg2 .extras import register_uuid , RealDictCursor , RealDictRow # type: ignore
1616from psycopg2 import sql
1717
18- from db_wrapper .connection import ConnectionParameters , connect
18+ from db_wrapper .connection import ConnectionParameters , get_pool
1919
2020# add uuid support to psycopg2 & Postgres
2121register_uuid ()
@@ -33,18 +33,19 @@ class AsyncClient:
3333 """
3434
3535 _connection_params : ConnectionParameters
36- _connection : aiopg .Connection
36+ _pool : aiopg .Pool
3737
3838 def __init__ (self , connection_params : ConnectionParameters ) -> None :
3939 self ._connection_params = connection_params
4040
4141 async def connect (self ) -> None :
42- """Connect to the database."""
43- self ._connection = await connect (self ._connection_params )
42+ """Create a database connection pool ."""
43+ self ._pool = await get_pool (self ._connection_params )
4444
4545 async def disconnect (self ) -> None :
46- """Disconnect from the database."""
47- await self ._connection .close ()
46+ """Close database connection pool."""
47+ self ._pool .close ()
48+ await self ._pool .wait_closed ()
4849
4950 # PENDS python 3.9 support in pylint
5051 # pylint: disable=unsubscriptable-object
@@ -81,7 +82,7 @@ async def execute(
8182 Returns:
8283 None
8384 """
84- async with self ._connection .cursor () as cursor :
85+ with ( await self ._pool .cursor (cursor_factory = RealDictCursor ) ) as cursor :
8586 await self ._execute_query (cursor , query , params )
8687
8788 # PENDS python 3.9 support in pylint
@@ -101,7 +102,7 @@ async def execute_and_return(
101102 Returns:
102103 List containing all the rows that matched the query.
103104 """
104- async with self ._connection .cursor () as cursor :
105+ with ( await self ._pool .cursor (cursor_factory = RealDictCursor ) ) as cursor :
105106 await self ._execute_query (cursor , query , params )
106107
107108 result : List [RealDictRow ] = await cursor .fetchall ()
0 commit comments