feat: use connection pool instead of a single shared connection
This is useful for multitask environments (like the API), to avoid conflicts
This commit is contained in:
parent
a22413bb0b
commit
aa72971627
5 changed files with 95 additions and 62 deletions
|
|
@ -2,12 +2,12 @@ from datetime import datetime
|
|||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
import psycopg2
|
||||
from psycopg2.extensions import connection
|
||||
|
||||
from domain.entities import ConsumptionPoint
|
||||
from domain.exceptions import DatabaseError
|
||||
from domain.value_objects import Granularity
|
||||
from ports.reading_query_repository import ReadingQueryRepository
|
||||
from infrastructure.db import get_conn
|
||||
|
||||
|
||||
_GRANULARITY_DELTA = {
|
||||
|
|
@ -19,9 +19,6 @@ _GRANULARITY_DELTA = {
|
|||
|
||||
|
||||
class PgReadingQueryRepository(ReadingQueryRepository):
|
||||
def __init__(self, conn: connection) -> None:
|
||||
self._conn = conn
|
||||
|
||||
def get_consumption(
|
||||
self,
|
||||
dev_eui: str,
|
||||
|
|
@ -54,9 +51,10 @@ class PgReadingQueryRepository(ReadingQueryRepository):
|
|||
ORDER BY period ASC
|
||||
"""
|
||||
try:
|
||||
with self._conn.cursor() as cur:
|
||||
cur.execute(query, (date_trunc, dev_eui, adjusted_start, end))
|
||||
rows = cur.fetchall()
|
||||
with get_conn() as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(query, (date_trunc, dev_eui, adjusted_start, end))
|
||||
rows = cur.fetchall()
|
||||
except psycopg2.DatabaseError as e:
|
||||
raise DatabaseError(f"Erreur requête consumption : {e}") from e
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue