What is a good way to organize your models, connections if one wants to use SQLAlchemy to connect several databases to various applications?

萝らか妹 提交于 2019-12-08 12:46:24

问题


Background:

This is the situation I am facing and so far my current solution seems rather clunky. I want to improve on it. Right now:

  • I setup connections to each database in the main function of the Pyramid application:
 def main(global_config, **settings):
    a_engine = engine_from_config(settings, 'A.')
    b_engine = engine_from_config(settings, 'B.')
    ASession.configure(bind=a_engine)
    BSession.configure(bind=b_engine)
  • "ASession" and "BSession" are simply globally defined scoped_session in /models/init.py.
ASession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
  • I define model base class like so. For example:
ABase = declarative_base()
class user(ABase):
   id = Column(Integer, primary_key=True)
   name = Column(String)

This somehow already doesn't feel very clean. But now that this model is supposed to be accessed from a different application, I also need to define the engine and connection again in that application. This feels extremely redundant.

Problem Abstracted:

Assume that there are 2 different databases:

A and B

Also assume that you want A and B to be accessible from 2 different applications (e.g.: Pyramid application, Bokeh Server App which uses Tornado) using the same model.

In short, how would one best pattern objects/models/classes/functions to produce clean non-redundant code in Python3?

Initial Thought After The Question Was Posted:

Thinking about this a bit more, I think I want each model to be somehow "self-contained". The model should bring with it methods for initiating connections. In other words, the initiation of db connections should be decoupled from the web application itself.

And it should be done in an instance kind of manner. So that multiple applications can use the same models. Each application would have its own session connection to either DB.

How would the community pattern this? Friday afternoons don't lend themselves to find answers to these kinds of questions for myself at least.


回答1:


I have done this. My recommendation below is how I like doing it, but is not the only way. I would ditch scoped sessions and the transaction manager and make explicit session management objects, with request lifecycle callbacks handling creation, closing, committing, or rolling back your sessions. Basically scoped sessions are a way to simulate a global by getting the same item for that thread of execution. The other way to do this in Pyramid is to attach things to the registry and the request, because you have those everywhere. You attach shared components to the registry (the ZCA) and per-request objects to the request.

When you have multiple sessions, I've found it much easier to reason about them and keep track of them if they are handled by components that wrap up everything for that engine. So for a case like that you describe, I've made two different DB engine components, that are created on start up, attached to the registry, and have a method for getting a fresh session. If you create these components properly, they should be usable in any application, whether it's Pyramid, Tornado, or your test script. You just make sure it has a constructor with some sane way of passing in settings for setting up the engine, whether it's a settings dict or kwargs. I then make my data model(s) live in their own python packages and it's easy to have any app in the family import the model, instantiate the engine components and go to town. Note that if you like using the ZCA registry (and I love it, it's a fantastic DI system), there's nothing preventing you from using it in non-pyramid apps, you just set it up manually in your server start up code.

In Pyramid specifically, I make a custom Request class and use the reify decorator to allow other pyramid code to get the session(s) for that request. The request class has end-of-life-callbacks attached to close out the sessions, and to do rollbacks or commits. There is a bit more boilerplate, but for me it's cleaner in that I can very easily trace where and when in code and time my session management is happening. It's also a good way for testing.

That said, there are lots of smart folks in SQLAlchemy/Pyramid land who swear by scoped sessions and the transaction manager, so there are other valid approaches. Hope that helps.



来源:https://stackoverflow.com/questions/44599402/what-is-a-good-way-to-organize-your-models-connections-if-one-wants-to-use-sqla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!