Avatar of chalie001
chalie001 asked on

connection pool in python

hi having this error when connection in python
/home/python/anaconda3/bin/python3.7 /home/python/Documents/intro-to-database/app.py

Traceback (most recent call last):

  File "/home/python/Documents/intro-to-database/app.py", line 6, in <module>

    my_user.save_to_db()

  File "/home/python/Documents/intro-to-database/user.py", line 18, in save_to_db

    (self.email, self.first_name, self.last_name))

  File "/home/python/Documents/intro-to-database/database.py", line 22, in __exit__

    self.connection_pool.putconn(self.connection)

AttributeError: 'connectionFromPool' object has no attribute 'connection_pool'

from psycopg2 import pool
 
connection_pool = pool.SimpleConnectionPool(1,
                                            1,
                                            dbname="learning",
                                            user="postgres",
                                            password="postgres",
                                            host="localhost",
                                            port="5432")
 
 
class connectionFromPool:
    def __init__(self):
        self.connection = None
 
    def __enter__(self):
        self.connection = connection_pool.getconn()
        return self.connection
 
    def __exit__(self, exc_type, exc_val, exc_tb):
        self.connection.commit()
        self.connection_pool.putconn(self.connection)

===
from database import connectionFromPool
 
 
class User:
    def __init__(self, first_name, email, last_name, id):
        self.email = email
        self.first_name = first_name
        self.last_name = last_name
        self.id = id
 
    def __repr__(self):
        return "<User {}>".format(self.email)
 
    def save_to_db(self):
        with connectionFromPool() as connection:
            with connection.cursor() as cursor:
                cursor.execute('INSERT INTO USERS(email,first_name,last_name) VALUES(%s,%s,%s)',
                               (self.email, self.first_name, self.last_name))
 
    @classmethod
    def load_from_db_by_email(cls, email):
        with connectionFromPool() as connection:
            with connection.cursor() as cursor:
                cursor.execute('select * from users where email=%s', (email,))
                user_data = cursor.fetchone()
                return cls(user_data[1], user_data[2], user_data[3], user_data[0])

Open in new window

* python3PostgreSQL* python2* pycharmPython

Avatar of undefined
Last Comment
Louis LIETAER

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Louis LIETAER

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Louis LIETAER

Any feed back ?
Louis LIETAER

Nice to have help you
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy