Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8084635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:51:25+00:00 2026-06-05T17:51:25+00:00

I am using master/slave of sqlalchemy, by RoutingSession like engines = { ‘master’:create_engine(sqlite:///master.db), ‘other’:create_engine(sqlite:///other.db),

  • 0

I am using master/slave of sqlalchemy, by RoutingSession like

engines = {
    'master':create_engine("sqlite:///master.db"),
    'other':create_engine("sqlite:///other.db"),
    'slave1':create_engine("sqlite:///slave1.db"),
    'slave2':create_engine("sqlite:///slave2.db"),
}

from sqlalchemy.orm import Session, sessionmaker
import random

class RoutingSession(Session):
    def get_bind(self, mapper=None, clause=None):
        if mapper and issubclass(mapper.class_, MyOtherClass):
            return engines['other']
        elif self._flushing:
            return engines['master']
        else:
            return engines[
                random.choice(['slave1','slave2'])
            ]

http://docs.sqlalchemy.org/en/rel_0_7/orm/session.html#custom-vertical-partitioning

I printed log in “get_bind” method. And I found the “get_bind” method was executed twice when using “session.add(r); session.commit()”.
So why twice?

When using “session.execute(‘insert …’); session.commit()”, the “get_bind” method was called only once.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:51:27+00:00Added an answer on June 5, 2026 at 5:51 pm

    The session supports multiple engines being associated at once with it, and these engines are associated with the session based on mappers or tables. Therefore anytime the Session needs to talk to the database, and it has in hand a specific mapper() to deal with, it must call get_bind() in order to get at the correct engine for this particular mapper. Within the flush, assuming dirty states exist (a result of session.add(r)), a particular mapper() is consulted more than once to get ready to emit SQL – at the moment at least once for the “save” pass and once for the “delete” pass though I can try to improve the unnecessary grab for the deletes in this case (edit: this is done).

    You can always tell why something is called when it is by sticking import pdb; pdb.set_trace() inside of it, then doing a “where” at the console each time you get there.

    So in general you should assume get_bind() is called all the time. It shouldn’t matter how often it’s called.

    Edit: the latest tip for 0.8 and 0.7 does not call get_bind() when there are no states to be flushed for a save/delete pass, so it’s now called once in this particular test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using propel master-dev with symfony 2.1. Is possible to write something like that
I'm running some maven builds in Jenkins, using a Master/multi-slave setup. These builds mostly
I am using ActiveMQ version 5.4 and I have a pure master slave configuration
After following the instruction to migrate from a GAE app from Master/Slave to High
I recently migrated an App Engine app from the master/slave datastore to the high
I am currently setting up a master-slave app using Ruby on Rails that needs
Using Bitten , can I use one machine (as master and slave) if I
I am using Java SDK of app-engine. I am using Master-slave datastore. I have
I am using IPython 0.12 for a master-slave style HPC task. I just switch
We have an existing application using the legacy Master-Slave database with an alias to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.