userver: en/testsuite/databases/pgsql/pool.py Source File
Loading...
Searching...
No Matches
pool.py
1import contextlib
2import typing
3
4import psycopg2.extensions
5import psycopg2.pool
6
7
9 def __init__(self, minconn: int, maxconn: int, uri: str) -> None:
10 self._pool = psycopg2.pool.ThreadedConnectionPool(minconn, maxconn, uri)
11
12 @contextlib.contextmanager
13 def get_connection(
14 self,
15 ) -> typing.Generator[psycopg2.extensions.connection, None, None]:
16 conn = self._pool.getconn()
17 conn.autocommit = True
18
19 try:
20 yield conn
21 finally:
22 self._pool.putconn(conn)
23
24 def close(self) -> None:
25 self._pool.closeall()