如何在 Django Python 中使用 PostgreSQL 为 SQLAlchemy 连接池设置方言?需要启用预 ping 功能
How do I set the dialect for a SQLAlchemy connection pool with PostgreSQL in Django Python? Needed for pre-ping feature enabled
我想要实现的目标
我需要为 Django 中的 SQLAlchemy 数据库池激活预 ping 功能 Python。
错误
当我将 pre_ping
属性 设置为 True
时,出现以下错误,提示我在使用预 ping 功能:
The ping feature requires that a dialect is passed to the connection pool.
代码
连接池代码creator/handler:
import psycopg2
from sqlalchemy import create_engine
import traceback
from myproject import settings
from dataClasses.EmptyObject import *
import json
import sqlalchemy.pool as pool
from sqlalchemy.pool import QueuePool
dbPool = None
class DbPoolHelper:
def ensurePoolCreated(self):
global dbPool
if dbPool != None:
return
self.createPool()
def dbConnect(self):
dbConfig = self.getDbPoolConfig()
dbConnection = psycopg2.connect(user=dbConfig.user, password=dbConfig.password, dbname=dbConfig.dbName, host=dbConfig.host, port=dbConfig.port)
print("=========== POOL CONNECTED =================")
return dbConnection
def createPool(self):
dbConnection = self.dbConnect
global dbPool
dbPool = pool.QueuePool(dbConnection, max_overflow=10, pool_size=5, pre_ping=True)
print("=========== POOL CREATED =================")
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
def getDbPoolConfig(self):
settingName = "pool"
dbConfig = EmptyObject()
dbConfig.host = settings.DATABASES[settingName]["HOST"]
dbConfig.port = settings.DATABASES[settingName]["PORT"]
dbConfig.user = settings.DATABASES[settingName]["USER"]
dbConfig.password = settings.DATABASES[settingName]["PASSWORD"]
dbConfig.dbName = settings.DATABASES[settingName]["NAME"]
dbConfig.poolSize = settings.DATABASES[settingName]["POOL_SIZE"]
dbConfig.poolMaxOverflow = settings.DATABASES[settingName]["POOL_MAX_OVERFLOW"]
dbConfig.poolRecycleTime = settings.DATABASES[settingName]["POOL_RECYCLE_TIME"]
return dbConfig
问题
如何在使用 PostgreSQL 数据库时将此 dialect
传递给连接池?
其他说明
如果您知道另一种在启用预 ping 功能的情况下创建连接池的方法,也欢迎这些建议。
谢谢!
我想要实现的目标
我需要为 Django 中的 SQLAlchemy 数据库池激活预 ping 功能 Python。
错误
当我将 pre_ping
属性 设置为 True
时,出现以下错误,提示我在使用预 ping 功能:
The ping feature requires that a dialect is passed to the connection pool.
代码
连接池代码creator/handler:
import psycopg2
from sqlalchemy import create_engine
import traceback
from myproject import settings
from dataClasses.EmptyObject import *
import json
import sqlalchemy.pool as pool
from sqlalchemy.pool import QueuePool
dbPool = None
class DbPoolHelper:
def ensurePoolCreated(self):
global dbPool
if dbPool != None:
return
self.createPool()
def dbConnect(self):
dbConfig = self.getDbPoolConfig()
dbConnection = psycopg2.connect(user=dbConfig.user, password=dbConfig.password, dbname=dbConfig.dbName, host=dbConfig.host, port=dbConfig.port)
print("=========== POOL CONNECTED =================")
return dbConnection
def createPool(self):
dbConnection = self.dbConnect
global dbPool
dbPool = pool.QueuePool(dbConnection, max_overflow=10, pool_size=5, pre_ping=True)
print("=========== POOL CREATED =================")
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
def getDbPoolConfig(self):
settingName = "pool"
dbConfig = EmptyObject()
dbConfig.host = settings.DATABASES[settingName]["HOST"]
dbConfig.port = settings.DATABASES[settingName]["PORT"]
dbConfig.user = settings.DATABASES[settingName]["USER"]
dbConfig.password = settings.DATABASES[settingName]["PASSWORD"]
dbConfig.dbName = settings.DATABASES[settingName]["NAME"]
dbConfig.poolSize = settings.DATABASES[settingName]["POOL_SIZE"]
dbConfig.poolMaxOverflow = settings.DATABASES[settingName]["POOL_MAX_OVERFLOW"]
dbConfig.poolRecycleTime = settings.DATABASES[settingName]["POOL_RECYCLE_TIME"]
return dbConfig
问题
如何在使用 PostgreSQL 数据库时将此 dialect
传递给连接池?
其他说明
如果您知道另一种在启用预 ping 功能的情况下创建连接池的方法,也欢迎这些建议。
谢谢!