We help IT Professionals succeed at work.

connection pool in python

73 Views
Last Modified: 2020-04-10
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

Comment
Watch Question

System Infrastructure Architect
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
Any feed back ?
Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
Nice to have help you

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.