def get_bind(self, mapper=None, clause=None): try: state = get_state(self.app) except (AssertionError, AttributeError, TypeError) as err: current_app.logger.info( "cant get configuration. default bind. Error:" + err) return orm.Session.get_bind(self, mapper, clause) """ If there are no binds configured, connect using the default SQLALCHEMY_DATABASE_URI """ if state is None or not self.app.config['SQLALCHEMY_BINDS']: if not self.app.debug: current_app.logger.info("Connecting -> DEFAULT") return orm.Session.get_bind(self, mapper, clause) # Writes go to the master elif self._flushing: # we who are about to write, salute you current_app.logger.info("Connecting -> MASTER") self.bind_key = self.master_key return state.db.get_engine(self.app, bind=self.bind_key) # Everything else goes to the slave else: current_app.logger.info("Connecting -> SLAVE") slave_key = random.choice(self.slave_keys) self.bind_key = slave_key return state.db.get_engine(self.app, bind=self.bind_key)
def get_bind(self, mapper, clause=None): binding_key = self.__find_binding_key(mapper) if binding_key is None: return BaseSignallingSession.get_bind(self, mapper, clause) else: state = get_state(self.app) return state.db.get_engine(self.app, bind=binding_key)
def get_bind(self, mapper=None, clause=None): try: state = get_state(self.app) except (AssertionError, AttributeError, TypeError) as err: log.info( "Unable to get Flask-SQLAlchemy configuration. Outputting default bind. Error:" + err) return orm.Session.get_bind(self, mapper, clause) # If there are no binds configured, connect using the default SQLALCHEMY_DATABASE_URI if state is None or not self.app.config['SQLALCHEMY_BINDS']: if not self.app.debug: log.debug( "Connecting -> DEFAULT. Unable to get Flask-SQLAlchemy bind configuration. Outputting default bind." ) return orm.Session.get_bind(self, mapper, clause) # Writes go to the master elif self._flushing: # we who are about to write, salute you log.debug("Connecting -> MASTER") return state.db.get_engine(self.app, bind='master') # Everything else goes to the slave else: log.debug("Connecting -> SLAVE") return state.db.get_engine(self.app, bind='slave')
def master_bind(self): try: state = get_state(self.app) except (AssertionError, AttributeError, TypeError) as err: current_app.logger.info( "cant get configuration. default bind. Error:" + err) raise RuntimeError("cant get configuration") return state.db.get_engine(self.app, bind=self.master_key)
def salve_bind(self): try: state = get_state(self.app) except (AssertionError, AttributeError, TypeError) as err: current_app.logger.info( "cant get configuration. default bind. Error:" + err) raise RuntimeError("cant get configuration") slave_key = random.choice(self.slave_keys) return state.db.get_engine(self.app, bind=slave_key)
def get_bind(self, mapper=None, clause=None): try: state = get_state(self.app) except (AssertionError, AttributeError, TypeError) as err: log.info( "Unable to get Flask-SQLAlchemy configuration. Outputting default bind. Error:" + err) return orm.Session.get_bind(self, mapper, clause) # If there are no binds configured, connect using the default SQLALCHEMY_DATABASE_URI if state is None or not self.app.config['SQLALCHEMY_BINDS']: if not self.app.debug: log.debug( "Connecting -> DEFAULT. Unable to get Flask-SQLAlchemy bind configuration. Outputting default bind." ) return orm.Session.get_bind(self, mapper, clause) has_bind = read_or_write.__dict__.has_key('read_or_write') if has_bind and read_or_write.__dict__['read_or_write'] == 'read': if 'read_type' in read_or_write.__dict__ and read_or_write.__dict__[ 'read_type'] == 'force_read': bind_key = read_or_write.__dict__['bind_key'] force_db_name = read_or_write.__dict__['force_db_name'] mapper_db_name = self.get_dbname(mapper, clause) if mapper_db_name == bind_key: # 若强制指定的从库,则走指定的从库 db_name = force_db_name else: db_name = mapper_db_name else: db_name = self.get_dbname(mapper, clause) if len(self.db_map[db_name]) == 0: # 抛异常,@read不能没有_slave_ raise Exception('请配置从库:' + db_name) pass slave_for_read = self.get_slave(db_name) return state.db.get_engine(self.app, bind=slave_for_read) else: if mapper is not None: info = getattr(mapper.mapped_table, 'info', {}) bind_key = info.get('bind_key') if bind_key is not None: return state.db.get_engine(self.app, bind=bind_key) return SessionBase.get_bind(self, mapper, clause)
def get_bind(self, mapper=None, clause=None): try: state = get_state(self.app) except (AssertionError, AttributeError, TypeError) as err: log.info("Unable to get Flask-SQLAlchemy configuration. Outputting default bind. Error:" + err) return orm.Session.get_bind(self, mapper, clause) # If there are no binds configured, connect using the default SQLALCHEMY_DATABASE_URI if state is None or not self.app.config['SQLALCHEMY_BINDS']: if not self.app.debug: log.debug("Connecting -> DEFAULT. Unable to get Flask-SQLAlchemy bind configuration. Outputting default bind." ) return orm.Session.get_bind(self, mapper, clause) # Writes go to the master elif self._flushing: # we who are about to write, salute you log.debug("Connecting -> MASTER") return state.db.get_engine(self.app, bind='master') # Everything else goes to the slave else: log.debug("Connecting -> SLAVE") return state.db.get_engine(self.app, bind='slave')
def _get_session(): return get_state(current_app).db.session