class documentation

class ShardedSession(Session): (source)

View In Hierarchy

Undocumented

Method __init__ Construct a ShardedSession.
Method bind_shard Undocumented
Method connection_callable Provide a :class:`_engine.Connection` to use in the unit of work flush process.
Method get_bind Return a "bind" to which this :class:`.Session` is bound.
Instance Variable execute_chooser Undocumented
Instance Variable identity_chooser Undocumented
Instance Variable shard_chooser Undocumented
Method _choose_shard_and_assign Undocumented
Method _identity_lookup override the default :meth:`.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).
Instance Variable __shards 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 Place an object into this :class:`_orm.Session`.
Method add_all Add the given collection of instances to this :class:`_orm.Session`.
Method begin Begin a transaction, or nested transaction, on this :class:`.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 :class:`_orm.Mapper` or arbitrary Python class with a "bind", e.g. an :class:`_engine.Engine` or :class:`_engine.Connection`.
Method bind_table Associate a :class:`_schema.Table` with a "bind", e.g. an :class:`_engine.Engine` or :class:`_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 :class:`_orm.Session`.
Method commit Flush pending changes and commit the current transaction.
Method connection Return a :class:`_engine.Connection` object corresponding to this :class:`.Session` object's transactional state.
Method delete Mark an instance as deleted.
Method enable_relationship_loading Associate an object with this :class:`.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 :class:`_orm.Session` has begun a nested transaction, e.g. SAVEPOINT.
Method in_transaction Return True if this :class:`_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 :class:`.Session`.
Method prepare Prepare the current transaction in progress for two phase commit.
Method query Return a new :class:`_query.Query` object corresponding to this :class:`_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 dispatch Undocumented
Instance Variable autobegin Undocumented
Instance Variable autoflush Undocumented
Instance Variable bind Undocumented
Instance Variable enable_baked_queries Undocumented
Instance Variable expire_on_commit Undocumented
Instance Variable hash_key Undocumented
Instance Variable identity_map A mapping of object identities to objects themselves.
Instance Variable join_transaction_mode Undocumented
Instance Variable twophase Undocumented
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 :class:`.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.
Method _add_bind Undocumented
Method _after_attach Undocumented
Method _autobegin_t 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 _execute_internal 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 _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
Class Variable _is_asyncio Undocumented
Class Variable _trans_context_manager Undocumented
Instance Variable __binds 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
Property _dirty_states The set of all persistent states considered dirty.

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 :class:`.Session` to which an object belongs.
def __init__(self, shard_chooser: ShardChooser, identity_chooser: Optional[IdentityChooser] = None, execute_chooser: Optional[Callable[[ORMExecuteState], Iterable[Any]]] = None, shards: Optional[Dict[str, Any]] = None, query_cls: Type[Query[_T]] = ShardedQuery, *, id_chooser: Optional[Callable[[Query[_T], Iterable[_T]], Iterable[Any]]] = None, query_chooser: Optional[Callable[[Executable], Iterable[Any]]] = None, **kwargs: Any): (source)

Construct a ShardedSession. :param shard_chooser: A 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. :param identity_chooser: A callable, passed a Mapper and primary key argument, which should return a list of shard ids where this primary key might reside. .. versionchanged:: 2.0 The ``identity_chooser`` parameter supersedes the ``id_chooser`` parameter. :param execute_chooser: For a given :class:`.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. .. versionchanged:: 1.4 The ``execute_chooser`` parameter supersedes the ``query_chooser`` parameter. :param shards: A dictionary of string shard names to :class:`~sqlalchemy.engine.Engine` objects.

def bind_shard(self, shard_id: ShardIdentifier, bind: Union[Engine, OptionEngine]): (source)

Undocumented

def connection_callable(self, mapper: Optional[Mapper[_T]] = None, instance: Optional[Any] = None, shard_id: Optional[ShardIdentifier] = None, **kw: Any) -> Connection: (source)

Provide a :class:`_engine.Connection` to use in the unit of work flush process.

def get_bind(self, mapper: Optional[_EntityBindKey[_O]] = None, *, shard_id: Optional[ShardIdentifier] = None, instance: Optional[Any] = None, clause: Optional[ClauseElement] = None, **kw: Any) -> _SessionBind: (source)

Return a "bind" to which this :class:`.Session` is bound. The "bind" is usually an instance of :class:`_engine.Engine`, except in the case where the :class:`.Session` has been explicitly bound directly to a :class:`_engine.Connection`. For a multiply-bound or unbound :class:`.Session`, the ``mapper`` or ``clause`` arguments are used to determine the appropriate bind to return. Note that the "mapper" argument is usually present when :meth:`.Session.get_bind` is called via an ORM operation such as a :meth:`.Session.query`, each individual INSERT/UPDATE/DELETE operation within a :meth:`.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 :class:`_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 :class:`_schema.MetaData` ultimately associated with the clause. 5. if mapper given, attempt to return a bind linked to the :class:`_schema.MetaData` ultimately associated with the :class:`_schema.Table` or other selectable to which the mapper is mapped. 6. No bind can be found, :exc:`~sqlalchemy.exc.UnboundExecutionError` is raised. Note that the :meth:`.Session.get_bind` method can be overridden on a user-defined subclass of :class:`.Session` to provide any kind of bind resolution scheme. See the example at :ref:`session_custom_partitioning`. :param mapper: Optional mapped class or corresponding :class:`_orm.Mapper` instance. The bind can be derived from a :class:`_orm.Mapper` first by consulting the "binds" map associated with this :class:`.Session`, and secondly by consulting the :class:`_schema.MetaData` associated with the :class:`_schema.Table` to which the :class:`_orm.Mapper` is mapped for a bind. :param clause: A :class:`_expression.ClauseElement` (i.e. :func:`_expression.select`, :func:`_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 :class:`_schema.Table` associated with bound :class:`_schema.MetaData`. .. seealso:: :ref:`session_partitioning` :paramref:`.Session.binds` :meth:`.Session.bind_mapper` :meth:`.Session.bind_table`

execute_chooser = (source)

Undocumented

identity_chooser = (source)

Undocumented

shard_chooser = (source)

Undocumented

def _choose_shard_and_assign(self, mapper: Optional[_EntityBindKey[_O]], instance: Any, **kw: Any) -> Any: (source)

Undocumented

def _identity_lookup(self, mapper: Mapper[_O], primary_key_identity: Union[Any, Tuple[Any, ...]], identity_token: Optional[Any] = None, passive: PassiveFlag = PassiveFlag.PASSIVE_OFF, lazy_loaded_from: Optional[InstanceState[Any]] = None, execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT, bind_arguments: Optional[_BindArguments] = None, **kw: Any) -> Union[Optional[_O], LoaderCallableStatus]: (source)

override the default :meth:`.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). .. versionchanged:: 1.4 Moved :meth:`.Session._identity_lookup` from the :class:`_query.Query` object to the :class:`.Session`.