[英]Database connection object is not callable exception thrown with Python Django SQL Alchemy database pooling. Why?
Create a database connection pool in Django.在 Django 中创建数据库连接池。 The connection pool is connected to a PostgreSQL database by using SQLAlchemy's connection pooling with django-postgrespool2 .
连接池通过使用SQLAlchemy 的连接池与django-postgrespool2连接到 PostgreSQL 数据库。
'psycopg2.extensions.connection' object is not callable
is thrown when running the following line of code poolConnection = dbPool.connect()
. 'psycopg2.extensions.connection' object is not callable
在运行以下代码行时抛出poolConnection = dbPool.connect()
。 Printing the dbPool
object and type displays <sqlalchemy.pool.impl.QueuePool object at 0x00000171832A72B0> <class 'sqlalchemy.pool.impl.QueuePool'>
打印
dbPool
object 并键入显示<sqlalchemy.pool.impl.QueuePool object at 0x00000171832A72B0> <class 'sqlalchemy.pool.impl.QueuePool'>
Database helper class which creates the connection to the PostgreSQL database and creates the connection pool:数据库助手 class 创建到 PostgreSQL 数据库的连接并创建连接池:
import psycopg2
from sqlalchemy import pool
import traceback
dbPool = None
class DbPoolHelper:
def ensurePoolCreated(self):
global dbPool
if dbPool != None:
return
self.createPool()
def dbConnect(self):
dbConnection = psycopg2.connect(user="...", password="...", dbname="...", host="...",port="...")
return dbConnection
def createPool(self):
dbConnection = self.dbConnect()
global dbPool
dbPool = pool.QueuePool(dbConnection, max_overflow=10, pool_size=5)
def execute(self, sql, sqlParams):
try:
global dbPool
self.ensurePoolCreated()
poolConnection = dbPool.connect()
cursor = poolConnection.cursor()
cursor.execute(sql, sqlParams)
poolConnection.commit()
result = cursor.fetchall()
cursor.close()
poolConnection.close()
return result
except Exception as e:
print(e)
return e
The code using the DbPoolHelper
to fetch some data by using the execute
method and giving some sql and sql params as arguments:代码使用
DbPoolHelper
通过execute
方法获取一些数据,并给出一些 sql 和 sql 参数作为 arguments:
def poolTest():
sql = "SELECT * FROM sysproductcontainers;"
sqlParams = ()
db = DbPoolHelper()
result = db.execute(sql, sqlParams)
Why does the code throw 'psycopg2.extensions.connection' object is not callable
?为什么代码会抛出
'psycopg2.extensions.connection' object is not callable
?
Keep in mind that I am pretty new to Python and Django.请记住,我对 Python 和 Django 还是很陌生。 So I might be missing something obvious to some Python and/or Django developers.
所以对于一些 Python 和/或 Django 开发人员来说,我可能会遗漏一些明显的东西。
Thanks!谢谢!
According to the QueuePool
docs the first argument should be 'a callable function that returns a DB-API connection object'.根据
QueuePool
文档,第一个参数应该是“返回 DB-API 连接对象的可调用 function”。
You've passed the result of the called function to the QueuePool
object as the first argument instead of the function itself.您已将调用 function 的结果作为第一个参数而不是 function 本身传递给
QueuePool
object。 Remove the parenthesis to solve the issue:删除括号以解决问题:
def createPool(self):
dbConnection = self.dbConnect
global dbPool
dbPool = pool.QueuePool(dbConnection, max_overflow=10, pool_size=5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.