I am trying to implement connection pooling with SQLAlchemy and pymysql:
import pymysql
import sqlalchemy.pool as pool
# Lifted from the pymysql docs: https://github.com/PyMySQL/PyMySQL/blob/master/example.py
cnx = pymysql.connect(host='localhost',user='xxxx',password='xxxx',db='mydatabase')
# Lifted straight from the SQLAlchemy docs http://docs.sqlalchemy.org/en/latest/core/pooling.html:
cnxPool = pool.QueuePool(cnx, max_overflow=10, pool_size=5)
conn = cnxPool.connect()
cursor = conn.cursor()
cursor.execute("select * from PUBLIC_URLS")
cursor.close()
I simply get the following error at the bottom of a fairly lengthy stack trace:
File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-1.1.3-py2.7-linux-x86_64.egg/sqlalchemy/pool.py", line 279, in <lambda> return lambda crec: creator() TypeError: 'Connection' object is not callable
Would anyone have any suggestion as to what's wrong?
Thanks
The first argument to QueuePool
must be a function that returns a new connection. It will be called whenever the pool needs a new connection.
You're passing a connection, not a function. When QueuePool
tries to call it, the error TypeError: 'Connection' object is not callable
occurs.
Try this instead:
...
def getconn():
return pymysql.connect(host='localhost',user='xxxx',password='xxxx',db='mydatabase')
cnxPool = pool.QueuePool(getconn, max_overflow=10, pool_size=5)
...