0

I have written a function for connecting to a database using pymysql. Here is my code:

def SQLreadrep(sql):
    connection=pymysql.connect(host=############,
                               user=#######,
                               password=########,
                               db=#########)
    with connection.cursor() as cursor:
            cursor.execute(sql)
            rows=cursor.fetchall()
    connection.commit()
    connection.close()
    return rows

I pass the SQL into this function and return the rows. However, I am doing quick queries to the database. (Something like "SELECT sku WHERE object='2J4423K').

  1. What is a way to avoid so many connections?
  2. Should I be avoiding this many connections to begin with?
  3. Could I crash a server using this many connections and queries?

1 Answer 1

3

Let me answer your last question first. Your function is acquiring a connection but it is closing it prior to returning. So, I see no reason why unless your were multithreading or multiprocessing you would ever be using more than one connection at a time and you should not be crashing the server.

The way to avoid the overhead of creating and closing so many connections would be to "cache" the connection. One way to do that would be to replace your function by a class:

import pymysql

class DB(object):
    def __init__(self, datasource, db_user, db_password):
        self.conn = pymysql.connect(db=datasource, user=db_user, password=db_password)

    def __del__(self):
        self.conn.close()

    def query(self, sql):
        with self.conn.cursor() as cursor:
            cursor.execute(sql)
            self.conn.commit()
            return cursor.fetchall()

Then you instantiate an instance of the DB class and invoke its query method. When the DB instance is grabage collected, the connection will be automatically closed.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I was concerned about the load of connections.
An alternate approach is to install a pymysql connection pool package from the Python Package Index (PyPI), such as pymysql-pooling. This would be a good approach if you were doing, for instance, multithreading and you did need multiple, simultaneous connections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.