Skip to content

Commit 03cdcf1

Browse files
committed
reload pool if connection with the db is lost
1 parent 3fed150 commit 03cdcf1

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

addok_psql_store/__init__.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import os
22

3-
from psycopg2 import pool
3+
from psycopg2 import pool, OperationalError, InterfaceError
44
from psycopg2.extras import execute_values
55

66
from addok.config import config
77

88

99
class PSQLStore:
1010
def __init__(self, *args, **kwargs):
11-
self.pool = pool.SimpleConnectionPool(minconn=8, maxconn=64,
11+
self.pool = pool.SimpleConnectionPool(minconn=1, maxconn=2,
1212
dsn=config.PG_CONFIG)
1313
create_table_query = '''
1414
CREATE TABLE IF NOT EXISTS
@@ -25,7 +25,13 @@ def __init__(self, *args, **kwargs):
2525
def getconn(self):
2626
# Use pid as connection id so we can reuse the connection within the
2727
# same process.
28-
return self.pool.getconn(key=os.getpid())
28+
conn = self.pool.getconn(key=os.getpid())
29+
try:
30+
c = conn.cursor()
31+
except (OperationalError, InterfaceError) as err:
32+
self.pool.putconn(conn, key=os.getpid())
33+
getconn()
34+
return conn
2935

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

0 commit comments

Comments
 (0)