<分区>
我在同时使用引擎、连接池和 session 时遇到问题。我想要做的是创建一个应用程序范围的引擎和池。然后随时从连接池创建 session 。下面是我的代码示例:
import sqlalchemy.pool as pool
engine = createEngine(pool_size, pool.QueuePool)
Session = sessionmaker()
def getConnection():
try:
return engine.connect()
except:
return False
mypool = pool.QueuePool(getConnection(), max_overflow, pool_size)
conn = mypool.connect()
session = Session(bind=conn)
result = session.query(Models.Features).all()
在上面的代码中,session.query() 给出了错误“error:'function' object has no attribute 'contextual_connect.
我在页面 https://docs.sqlalchemy.org/en/latest/orm/session_basics.html 中使用了标题为“使用替代参数创建临时 session 对象”下的段落作为引用。这是我使用的上述页面的代码示例:
# at the module level, the global sessionmaker,
# bound to a specific Engine
Session = sessionmaker(bind=engine)
# later, some unit of code wants to create a
# Session that is bound to a specific Connection
conn = engine.connect()
session = Session(bind=conn)
在上面的示例中,他们将 session 与连接绑定(bind)在一起。我想知道在使用session的时候是否需要使用connectionpool。如果是,那么如何创建连接池并将 session 与从池中获得的连接绑定(bind)。