troubleshooting Question

connection pool in python

Avatar of chalie001
chalie001 asked on
PythonPostgreSQL* pycharm* python3* python2
3 Comments1 Solution91 ViewsLast Modified:
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])
ASKER CERTIFIED SOLUTION
Louis LIETAER
System Infrastructure Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros