Hello TG Group,
Examining TG2.2 I found that now it has an option to support load balancing using SQLAlchemy Master Slave Load Balancing.
What it does is to subclass sqlalchemy.orm.Session, overrides get_bind method, and sets it in the sessionmaker as class_; much like A. Molina explain in other post but now I kind of get it.
I'm interested in beeing able to select a database depending from where my app gets the request. So I figure to copy the balanced session method and adapt it to my needs.
I have two scenarios,
The request comes from:
http://db_one.myapp.com I want to select the database db_one ------
The request comes from:
------
In the first one
db_url = config['sqlalchemy.url'].replace('dbname', dbname)
engine = create_engine(db_url, pool_recycle=3600)
The second one I haven't realized yet, maybe I'll try with a parameter like ac=db_one on the url.
When I try to implement this for the first scenario I get
'TypeError: No object (name: request) has been registered for this thread'
When the method is called 'request' object is 'request: <paste.registry.StackedObjectProxy object at 0x90a4e0c>' meaning is not yet defined.
Looking at the TG stack
WSGI Server
PasteCascade - serves one of a list of WSGI apps.
StaticFile Server - serves static files from /public
OR
TurboGears Application: - the TG stack
Registry Manager - sets up the request proxy, etc.
Error Middleware - if the path goes to _debug handle the request
Database Session Manager - setup the DBSession
Transaction Manager -
Authentication - add info to the environ if user is authenticated
Authorization - add more info to the environ for authorization.
ToscaWidgets - nothing on the way in.
Cache - sets up the cache
Session - sets up the web session
Routes - parses the URL and adds info to environ
Custom Middleware - User defined middleware
TurboGearsApp – calls WSGI style controller
ObjectDispatchController – gets params, do validation, etc
Your Controller Code – does anything!
ObjectDispatchController – renders response, etc.
ToscaWidgets - injects resources used by widgets
Transaction Manager - commits or rolls back transaction
Database Session Manager - cleans up the DBSession
Error Middleware - displays error pages, etc
The request proxy is set before the db session manager so I don't know why is request getting called before assignment.
Any orientation will be appreciated.
Thanks