class documentation

class ShardedSession(Session):

View In Hierarchy

Undocumented

Method __init__ Construct a ShardedSession.
Method connection​_callable Provide a _engine.Connection to use in the unit of work flush process.
Method get​_bind Return a "bind" to which this .Session is bound.
Method ​_choose​_shard​_and​_assign Undocumented
Method ​_identity​_lookup override the default .Session._identity_lookup method so that we search for a given non-token primary key identity across all possible identity tokens (e.g. shard ids).
Method bind​_shard Undocumented
Instance Variable __binds Undocumented
Instance Variable execute​_chooser Undocumented
Instance Variable id​_chooser Undocumented
Instance Variable query​_chooser Undocumented
Instance Variable shard​_chooser Undocumented

Inherited from Session:

Method __contains__ Return True if the instance is associated with this session.
Method __enter__ Undocumented
Method __exit__ Undocumented
Method __iter__ Iterate over all pending or persistent instances within this Session.
Method ​_add​_bind Undocumented
Method ​_after​_attach Undocumented
Method ​_autobegin Undocumented
Method ​_autoflush Undocumented
Method ​_before​_attach Undocumented
Method ​_bulk​_save​_mappings Undocumented
Method ​_close​_impl Undocumented
Method ​_conditional​_expire Expire a state if persistent, else expunge if pending
Method ​_connection​_for​_bind Undocumented
Method ​_contains​_state Undocumented
Method ​_delete​_impl Undocumented
Method ​_expire​_state Undocumented
Method ​_expunge​_states Undocumented
Method ​_flush Undocumented
Method ​_flush​_warning Undocumented
Method ​_get​_impl Undocumented
Method ​_is​_clean Undocumented
Method ​_legacy​_transaction Undocumented
Method ​_maker​_context​_manager Undocumented
Method ​_merge Undocumented
Method ​_register​_altered Undocumented
Method ​_register​_persistent Register all persistent objects from a flush.
Method ​_remove​_newly​_deleted Undocumented
Method ​_save​_impl Undocumented
Method ​_save​_or​_update​_impl Undocumented
Method ​_save​_or​_update​_state Undocumented
Method ​_update​_impl Undocumented
Method ​_validate​_persistent Undocumented
Method add Place an object in the Session.
Method add​_all Add the given collection of instances to this Session.
Method begin Begin a transaction, or nested transaction, on this .Session, if one is not already begun.
Method begin​_nested Begin a "nested" transaction on this Session, e.g. SAVEPOINT.
Method bind​_mapper Associate a _orm.Mapper or arbitrary Python class with a "bind", e.g. an _engine.Engine or _engine.Connection.
Method bind​_table Associate a _schema.Table with a "bind", e.g. an _engine.Engine or _engine.Connection.
Method bulk​_insert​_mappings Perform a bulk insert of the given list of mapping dictionaries.
Method bulk​_save​_objects Perform a bulk save of the given list of objects.
Method bulk​_update​_mappings Perform a bulk update of the given list of mapping dictionaries.
Method close Close out the transactional resources and ORM objects used by this _orm.Session.
Method commit Flush pending changes and commit the current transaction.
Method connection Return a _engine.Connection object corresponding to this .Session object's transactional state.
Method delete Mark an instance as deleted.
Method enable​_relationship​_loading Associate an object with this .Session for related object loading.
Method execute Execute a SQL expression construct.
Method expire Expire the attributes on an instance.
Method expire​_all Expires all persistent instances within this Session.
Method expunge Remove the instance from this Session.
Method expunge​_all Remove all object instances from this Session.
Method flush Flush all the object changes to the database.
Method get Return an instance based on the given primary key identifier, or None if not found.
Method get​_nested​_transaction Return the current nested transaction in progress, if any.
Method get​_transaction Return the current root transaction in progress, if any.
Method in​_nested​_transaction Return True if this _orm.Session has begun a nested transaction, e.g. SAVEPOINT.
Method in​_transaction Return True if this _orm.Session has begun a transaction.
Method invalidate Close this Session, using connection invalidation.
Method is​_modified Return True if the given instance has locally modified attributes.
Method merge Copy the state of a given instance into a corresponding instance within this .Session.
Method prepare Prepare the current transaction in progress for two phase commit.
Method query Return a new _query.Query object corresponding to this _orm.Session.
Method refresh Expire and refresh attributes on the given instance.
Method rollback Rollback the current transaction in progress.
Method scalar Execute a statement and return a scalar result.
Method scalars Execute a statement and return the results as scalars.
Class Variable ​_is​_asyncio Undocumented
Class Variable ​_trans​_context​_manager Undocumented
Instance Variable ​_deleted Undocumented
Instance Variable ​_flushing Undocumented
Instance Variable ​_nested​_transaction Undocumented
Instance Variable ​_new Undocumented
Instance Variable ​_query​_cls Undocumented
Instance Variable ​_transaction Undocumented
Instance Variable ​_warn​_on​_events Undocumented
Instance Variable autocommit Undocumented
Instance Variable autoflush Undocumented
Instance Variable bind Undocumented
Instance Variable enable​_baked​_queries Undocumented
Instance Variable expire​_on​_commit Undocumented
Instance Variable future Undocumented
Instance Variable hash​_key Undocumented
Instance Variable identity​_map A mapping of object identities to objects themselves.
Instance Variable twophase Undocumented
Property ​_dirty​_states The set of all persistent states considered dirty.
Property deleted The set of all instances marked as 'deleted' within this Session
Property dirty The set of all persistent instances considered dirty.
Property info A user-modifiable dictionary.
Property is​_active True if this .Session not in "partial rollback" state.
Property new The set of all instances marked as 'new' within this Session.
Property no​_autoflush Return a context manager that disables autoflush.
Property transaction The current active or inactive .SessionTransaction.

Inherited from _SessionClassMethods (via Session):

Class Method close​_all Close all sessions in memory.
Class Method identity​_key Return an identity key.
Class Method object​_session Return the .Session to which an object belongs.
def __init__(self, shard_chooser, id_chooser, execute_chooser=None, shards=None, query_cls=ShardedQuery, **kwargs):
Construct a ShardedSession.
Parameters
shard​_chooserA callable which, passed a Mapper, a mapped instance, and possibly a SQL clause, returns a shard ID. This id may be based off of the attributes present within the object, or on some round-robin scheme. If the scheme is based on a selection, it should set whatever state on the instance to mark it in the future as participating in that shard.
id​_chooserA callable, passed a query and a tuple of identity values, which should return a list of shard ids where the ID might reside. The databases will be queried in the order of this listing.
execute​_chooser

For a given .ORMExecuteState, returns the list of shard_ids where the query should be issued. Results from all shards returned will be combined together into a single listing.

Changed in version 1.4: The execute_chooser parameter supersedes the query_chooser parameter.
shardsA dictionary of string shard names to ~sqlalchemy.engine.Engine objects.
query​_clsUndocumented
**kwargsUndocumented
def connection_callable(self, mapper=None, instance=None, shard_id=None, **kwargs):
Provide a _engine.Connection to use in the unit of work flush process.
def get_bind(self, mapper=None, shard_id=None, instance=None, clause=None, **kw):

Return a "bind" to which this .Session is bound.

The "bind" is usually an instance of _engine.Engine, except in the case where the .Session has been explicitly bound directly to a _engine.Connection.

For a multiply-bound or unbound .Session, the mapper or clause arguments are used to determine the appropriate bind to return.

Note that the "mapper" argument is usually present when .Session.get_bind is called via an ORM operation such as a .Session.query, each individual INSERT/UPDATE/DELETE operation within a .Session.flush, call, etc.

The order of resolution is:

  1. if mapper given and :paramref:`.Session.binds` is present, locate a bind based first on the mapper in use, then on the mapped class in use, then on any base classes that are present in the __mro__ of the mapped class, from more specific superclasses to more general.
  2. if clause given and Session.binds is present, locate a bind based on _schema.Table objects found in the given clause present in Session.binds.
  3. if Session.binds is present, return that.
  4. if clause given, attempt to return a bind linked to the _schema.MetaData ultimately associated with the clause.
  5. if mapper given, attempt to return a bind linked to the _schema.MetaData ultimately associated with the _schema.Table or other selectable to which the mapper is mapped.
  6. No bind can be found, ~sqlalchemy.exc.UnboundExecutionError is raised.

Note that the .Session.get_bind method can be overridden on a user-defined subclass of .Session to provide any kind of bind resolution scheme. See the example at :ref:`session_custom_partitioning`.

See Also

:ref:`session_partitioning`

:paramref:`.Session.binds`

.Session.bind_mapper

.Session.bind_table

Parameters
mapperOptional .mapper mapped class or instance of _orm.Mapper. The bind can be derived from a _orm.Mapper first by consulting the "binds" map associated with this .Session, and secondly by consulting the _schema.MetaData associated with the _schema.Table to which the _orm.Mapper is mapped for a bind.
shard​_idUndocumented
instanceUndocumented
clauseA _expression.ClauseElement (i.e. _expression.select, _expression.text, etc.). If the mapper argument is not present or could not produce a bind, the given expression construct will be searched for a bound element, typically a _schema.Table associated with bound _schema.MetaData.
**kwUndocumented
def _choose_shard_and_assign(self, mapper, instance, **kw):

Undocumented

def _identity_lookup(self, mapper, primary_key_identity, identity_token=None, lazy_loaded_from=None, **kw):

override the default .Session._identity_lookup method so that we search for a given non-token primary key identity across all possible identity tokens (e.g. shard ids).

Changed in version 1.4: Moved .Session._identity_lookup from the _query.Query object to the .Session.
def bind_shard(self, shard_id, bind):

Undocumented

__binds: dict =
execute_chooser =

Undocumented

id_chooser =

Undocumented

query_chooser =

Undocumented

shard_chooser =

Undocumented