Skip to content

Commit

Permalink
reload pool if connection with the db is lost
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptobioz committed Feb 4, 2020
1 parent 3fed150 commit 3c2874b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions addok_psql_store/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os

from psycopg2 import pool
from psycopg2 import pool, OperationalError, InterfaceError
from psycopg2.extras import execute_values

from addok.config import config


class PSQLStore:
def __init__(self, *args, **kwargs):
self.pool = pool.SimpleConnectionPool(minconn=8, maxconn=64,
self.pool = pool.SimpleConnectionPool(minconn=1, maxconn=2,
dsn=config.PG_CONFIG)
create_table_query = '''
CREATE TABLE IF NOT EXISTS
Expand All @@ -25,7 +25,13 @@ def __init__(self, *args, **kwargs):
def getconn(self):
# Use pid as connection id so we can reuse the connection within the
# same process.
return self.pool.getconn(key=os.getpid())
conn = self.pool.getconn(key=os.getpid())
try:
c = conn.cursor()
except (OperationalError, InterfaceError) as err:
self.pool.putconn(conn, key=os.getpid())
return self.getconn()
return conn

def fetch(self, *keys):
# Using ANY results in valid SQL if `keys` is empty.
Expand Down

0 comments on commit 3c2874b

Please sign in to comment.