'Flask Python How to Single Folder Multiple DB Connection

As we have multiple databases created for each customer, based on the session value of logged in customer, we are opening db connections in each and every function. Code as follows,

company_code = session['company_code']

filename = main_dir_path+"/"+company_code+".txt"
fp = open(filename)
for i, line in enumerate(fp):

    if i == 1:
        site_admin_db = line

    if i == 4:
        ams_db = line

site_admin_db = site_admin_db.strip()
ams_db = ams_db.strip()

engine1 = create_engine('postgresql+psycopg2://username:pwd@host/'+site_admin_db)
engine4 = create_engine('postgresql+psycopg2://username:pwd@host/'+ams_db)

Base = declarative_base()
Session1 = sessionmaker(bind=engine1)
Session4 = sessionmaker(bind=engine4)

sessions1 = Session1()
sessions4 = Session4()

max connections in postgresql server is 100, psycopg2 - operational error - too many clients error is thrown.

Whether increasing max connections value is good approach? or how to handle multiple db connections for different customer with single code folder.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source