将全局对象(映射器,元数据)绑定到用户特定的连接不是一种好方法。以及使用作用域会话。我建议为每个请求创建新会话并将其配置为使用用户特定的连接。下面的示例假定您对每个数据库使用单独的元数据对象:
binds = {}finance_engine = create_engine(url1)binds.update(dict.fromkeys(finance_metadata.sorted_tables, finance_engine))# The following line is required when mappings to joint tables are used (e.g.# in joint table inheritance) due to bug (or misfeature) in SQLAlchemy 0.5.4.# This issue might be fixed in newer versions.binds.update(dict.fromkeys([Employee, Customer, Invoice], finance_engine))staff_engine = create_engine(url2)binds.update(dict.fromkeys(staff_metadata.sorted_tables, staff_engine))# See comment above.binds.update(dict.fromkeys([Project, Hour], staff_engine))session = sessionmaker(binds=binds)()


