【问题标题】:How to set module name in SQLAlchemy and cx_Oracle with create_engine()?如何使用 create_engine() 在 SQLAlchemy 和 cx_Oracle 中设置模块名称?
【发布时间】:2021-12-08 11:01:13
【问题描述】:

在 SQLAlchemy 中,我创建了一个引擎:

engine = create_engine(url="oracle+cx_oracle://user:xxxx@tns")

在 cx_Oracle 中,我将创建一个连接:

conn = cx_Oracle.connect(user="user", password="xxxx", dsn="tns")

然后我可以使用Connection.module 属性设置模块,该属性在查看 v$session 表时会适当地标记。

conn.module = "MyModule"

有没有办法在使用create_engine 创建 Oracle 会话模块名称后将其设置为 sqlalchemy.engine.Engine

【问题讨论】:

标签: python sqlalchemy cx-oracle


【解决方案1】:

我最终使用了 DialectEvents.do_connect() hook,这对我来说效果很好。

import cx_Oracle
from sqlalchemy import create_engine, event


engine = create_engine(url="oracle+cx_oracle://user:xxxx@tns")
@event.listens_for(engine, "do_connect")
def receive_do_connect(dialect, conn_rec, cargs, cparams):
    """listen for the 'do_connect' event"""
    connection = cx_Oracle.connect(*cargs, **cparams)
    connection.module = "MyModule"
    return connection

【讨论】:

  • 感谢分享。如果您(或其他读者)在服务或 Web 应用程序中处理多个用户,则建议始终使用具有一些死连接检测功能并支持其他 Oracle 功能以实现高可用性的 Oracle 会话池。
猜你喜欢
  • 2015-12-20
  • 2018-07-21
  • 2021-03-11
  • 2018-07-30
  • 1970-01-01
  • 2016-12-17
  • 2022-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多