forked from GDXN/MangaCMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbPool.py
29 lines (19 loc) · 839 Bytes
/
dbPool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import psycopg2.pool
import settings
class ConnectionPool():
_shared_state = {}
_db_max_connections = 50
def __init__(self):
self.__dict__ = self._shared_state
if not hasattr(self, 'connected'):
print("Database connection pool init!")
try:
self.dbPool = psycopg2.pool.ThreadedConnectionPool(1, self._db_max_connections, dbname=settings.DATABASE_DB_NAME, user=settings.DATABASE_USER,password=settings.DATABASE_PASS)
except psycopg2.OperationalError:
self.dbPool = psycopg2.pool.ThreadedConnectionPool(1, self._db_max_connections, host=settings.DATABASE_IP, dbname=settings.DATABASE_DB_NAME, user=settings.DATABASE_USER,password=settings.DATABASE_PASS)
self.connected = True
def getconn(self):
return self.dbPool.getconn()
def putconn(self, conn):
self.dbPool.putconn(conn)
pool = ConnectionPool()