Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pool = ThreadedConnectionPool(
- 1, 20, database=url.path,
- user=url.username,
- password=url.password,
- host=url.hostname,
- port=url.port
- )
- @contextmanager
- def db_connection():
- try:
- con = pool.getconn()
- yield con
- finally:
- pool.putconn(con)
- @contextmanager
- def db_cursor(commit=False):
- with db_connection() as con:
- cur = con.cursor(cursor_factory=RealDictCursor)
- try:
- yield cur
- if commit:
- con.commit()
- except psycopg2.DatabaseError as e:
- logging.getLogger(__name__).error('Error during using db: %s', e)
- con.rollback()
- finally:
- cur.close()
Add Comment
Please, Sign In to add comment