title: With sqlalchemy how to dynamically bind to database engine on a per-request basis
body: <p>I have a Pylons-based web application which connects via <a href="http://www.sqlalchemy.org/" rel="noreferrer">Sqlalchemy</a> (v0.5) to a Postgres database.  For security, rather than follow the typical pattern of simple web apps (as seen in just about all tutorials), I'm not using a generic Postgres user (e.g. "webapp") but am requiring that users enter their own Postgres userid and password, and am using that to establish the connection.  That means we get the full benefit of Postgres security.</p>
  
  <p>Complicating things still further, there are two separate databases to connect to.  Although they're currently in the same Postgres cluster, they need to be able to move to separate hosts at a later date.</p>
  
  <p>We're using sqlalchemy's <a href="http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html" rel="noreferrer">declarative</a> package, though I can't see that this has any bearing on the matter.</p>
  
  <p>Most examples of sqlalchemy show trivial approaches such as setting up the Metadata once, at application startup, with a generic database userid and password, which is used through the web application.  This is usually done with Metadata.bind = create_engine(), sometimes even at module-level in the database model files.</p>
  
  <p>My question is, how can we defer establishing the connections until the user has logged in, and then (of course) re-use those connections, or re-establish them using the same credentials, for each subsequent request.</p>
  
  <p>We have this working -- we think -- but I'm not only not certain of the safety of it, I also think it looks incredibly heavy-weight for the situation.</p>
  
  <p>Inside the <code>__call__</code> method of the BaseController we retrieve the userid and password from the web session, call sqlalchemy create_engine() once for each database, then call a routine which calls Session.bind_mapper() repeatedly, once for each table that <em>may</em> be referenced on each of those connections, even though any given request usually references only one or two tables.  It looks something like this:</p>
  
  <pre><code># in lib/base.py on the BaseController class
  def __call__(self, environ, start_response):
  
      # note: web session contains {'username': XXX, 'password': YYY}
      url1 = 'postgres://%(username)s:%(password)s@server1/finance' % session
      url2 = 'postgres://%(username)s:%(password)s@server2/staff' % session
  
      finance = create_engine(url1)
      staff = create_engine(url2)
      db_configure(staff, finance)  # see below
      ... etc
  
  # in another file
  
  Session = scoped_session(sessionmaker())
  
  def db_configure(staff, finance):
      s = Session()
  
      from db.finance import Employee, Customer, Invoice
      for c in [
          Employee,
          Customer,
          Invoice,
          ]:
          s.bind_mapper(c, finance)
  
      from db.staff import Project, Hour
      for c in [
          Project,
          Hour,
          ]:
          s.bind_mapper(c, staff)
  
      s.close()  # prevents leaking connections between sessions?
  </code></pre>
  
  <p>So the create_engine() calls occur on every request...  I can see that being needed, and the Connection Pool probably caches them and does things sensibly.  </p>
  
  <p>But calling Session.bind_mapper() once for <em>each</em> table, on <em>every</em> request?  Seems like there has to be a better way.</p>
  
  <p>Obviously, since a desire for strong security underlies all this, we don't want any chance that a connection established for a high-security user will inadvertently be used in a later request by a low-security user.</p>