【问题标题】:How can I use multiple databases in the same request in Cherrypy and SQLAlchemy?如何在 Cherrypy 和 SQLAlchemy 的同一个请求中使用多个数据库?
【发布时间】:2010-09-03 10:31:31
【问题描述】:

我的应用使用类似于this 的技术连接到多个数据库。只要我不尝试在同一个请求中访问不同的数据库,它就可以工作。回头看上面的脚本,我看到他们为此写了一条评论:

SQLAlchemy integration for CherryPy,
such that you can access multiple databases,
but only one of these databases per request or thread.

我的应用程序现在要求我从数据库 A 和数据库 B 中获取数据。是否可以在单个请求中执行此操作?

请参阅下面的来源和示例:

工作示例 1:

from model import meta

my_object_instance = meta.main_session().query(MyObject).filter(
    MyObject.id == 1
).one()

工作示例 2:

from model import meta

my_user = meta.user_session().query(User).filter(
    User.id == 1
).one()

错误示例:

from model import meta

my_object_instance = meta.main_session().query(MyObject).filter(
    MyObject.id == 1
).one()

my_user = meta.user_session().query(User).filter(
    User.id == 1
).one()

此错误与:

(sqlalchemy.exc.ProgrammingError) (1146, "Table 'main_db.user' doesn't exist")

来源:

# meta.py
import cherrypy
import sqlalchemy
from sqlalchemy import MetaData
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

# Return an Engine
def create_engine(defaultschema = True, schema = "", **kwargs):

    # A blank DB is the same as no DB so to specify a non-schema-specific connection just override with defaultschema = False
    connectionString = 'mysql://%s:%s@%s/%s?charset=utf8' % (
        store['application'].config['main']['database-server-config-username'],
        store['application'].config['main']['database-server-config-password'],
        store['application'].config['main']['database-server-config-host'],
        store['application'].config['main']['database-server-config-defaultschema'] if defaultschema else schema
    )
    # Create engine object. we pass **kwargs through so this call can be extended
    return sqlalchemy.create_engine(connectionString, echo=True, pool_recycle=10, echo_pool=True, encoding='utf-8', **kwargs)

# Engines
main_engine = create_engine()
user_engine = None

# Sessions
_main_session = None
_user_session = None

# Metadata
main_metadata = MetaData()
main_metadata.bind = main_engine
user_metadata = MetaData()

# No idea what bases are/do but nothing works without them
main_base = declarative_base(metadata = main_metadata)
user_base = declarative_base(metadata = user_metadata)

# An easy collection of user database connections
engines = {}

# Each thread gets a session based on this object
GlobalSession = scoped_session(sessionmaker(autoflush=True, autocommit=False, expire_on_commit=False))

def main_session():
    _main_session = cherrypy.request.main_dbsession
    _main_session.configure(bind=main_engine)

    return _main_session

def user_session():
    _user_session = cherrypy.request.user_dbsession
    _user_session.configure(bind = get_user_engine())

    return _user_session

def get_user_engine():

    # Get dburi from the users instance
    dburi = cherrypy.session['auth']['user'].instance.database

    # Store this engine for future use
    if dburi in engines:
        engine = engines.get(dburi)
    else:
        engine = engines[dburi] = create_engine(defaultschema = False, schema = dburi)

    # Return Engine
    return engine


def get_user_metadata():
    user_metadata.bind = get_user_engine()
    return user_metadata

# open a new session for the life of the request
def open_dbsession():
    cherrypy.request.user_dbsession = cherrypy.thread_data.scoped_session_class
    cherrypy.request.main_dbsession = cherrypy.thread_data.scoped_session_class
    return

# close the session for this request
def close_dbsession():
    if hasattr(cherrypy.request, "user_dbsession"):
        try:
            cherrypy.request.user_dbsession.flush()
            cherrypy.request.user_dbsession.remove()
            del cherrypy.request.user_dbsession
        except:
            pass
    if hasattr(cherrypy.request, "main_dbsession"):
        try:
            cherrypy.request.main_dbsession.flush()
            cherrypy.request.main_dbsession.remove()
            del cherrypy.request.main_dbsession
        except:
            pass

    return

# initialize the session factory class for the selected thread
def connect(thread_index):
    cherrypy.thread_data.scoped_session_class = scoped_session(sessionmaker(autoflush=True, autocommit=False))
    return

# add the hooks to cherrypy
cherrypy.tools.dbsession_open = cherrypy.Tool('on_start_resource', open_dbsession)
cherrypy.tools.dbsession_close = cherrypy.Tool('on_end_resource', close_dbsession)
cherrypy.engine.subscribe('start_thread', connect)

【问题讨论】:

  • 我不想“回答”我自己的问题,但解决方案似乎是在 connect 中为其他数据库添加额外的 scoped_session ,因此它看起来像 def connect(thread_index): cherrypy.thread_data.user_scoped_session_class = scoped_session(sessionmaker(autoflush=True, autocommit=False)) cherrypy.thread_data.main_scoped_session_class = scoped_session(sessionmaker(autoflush=True, autocommit=False)) return 然后引用它们分别在open_dbsession

标签: python sqlalchemy cherrypy


【解决方案1】:

您还可以选择从头开始为多个数据库设计的 ORM,例如 Dejavu

【讨论】:

    【解决方案2】:

    看看这个:

    http://pythonhosted.org/Flask-SQLAlchemy/binds.html

    基本上,它建议您为每个连接使用绑定参数。也就是说,这似乎是一个黑客攻击。

    这个问题在答案中有更多细节:

    With sqlalchemy how to dynamically bind to database engine on a per-request basis

    也就是说,这个问题和引用的问题都不是最新的,从那时起 sqlalchemy 可能会继续前进。

    【讨论】:

    • 看起来不错。我最终解决了我在这个问题上面临的直接问题(请参阅我对问题的评论),但最终遇到了几个进一步的问题;现在一切正常,但附带的代码和解释对于 StackOverflow 来说太大了。多个线程+每个用户多个动态数据库连接+每个请求设置/拆除=痛苦
    猜你喜欢
    • 2015-10-07
    • 2021-01-03
    • 2010-10-27
    • 2016-09-21
    • 2020-08-20
    • 2012-06-07
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多