aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2024-06-28 23:25:48 +0300
committerJan Tuomi <jan@jantuomi.fi>2024-12-21 19:43:13 +0200
commitdd5ea8ca68f9f006b903e4b026172006cbaf8e18 (patch)
treed6bb572f0c533832d86960a6590989cfb12b397e
parent737302e1c18c71ac55c7e241c8ef30003ad64f97 (diff)
Add recommended Postgres connection options to reduce disconnections
-rw-r--r--db.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/db.py b/db.py
index eb7204b..f09a76a 100644
--- a/db.py
+++ b/db.py
@@ -33,7 +33,23 @@ class Db:
database=os.getenv('DB_DATABASE', 'postgres'),
port=os.getenv('DB_PORT', '5432'),
user=os.getenv('DB_USER', 'postgres'),
- password=os.environ['DB_PASSWORD'])
+ password=os.environ['DB_PASSWORD'],
+
+ keepalives=1,
+ keepalives_idle=30,
+ keepalives_interval=10,
+ keepalives_count=5,
+
+ application_name='diddle',
+ connect_timeout=10,
+ sslmode='prefer',
+ client_encoding='UTF8',
+ )
+
+ # Set the transaction isolation level
+ with self.conn.cursor() as cursor:
+ cursor.execute("SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED")
+ self.conn.commit()
def get_cursor(self):
for _ in range(self.MAX_RETRIES):