Vous avez des améliorations (ou des corrections) à proposer pour ce document :
je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Return a new :class:`_engine.Engine` that will provide
:class:`_engine.Connection` objects with the given execution options.
The returned :class:`_engine.Engine` remains related to the original
:class:`_engine.Engine` in that it shares the same connection pool and
other state:
* The :class:`_pool.Pool` used by the new :class:`_engine.Engine`
is the
same instance. The :meth:`_engine.Engine.dispose`
method will replace
the connection pool instance for the parent engine as well
as this one.
* Event listeners are "cascaded" - meaning, the new
:class:`_engine.Engine`
inherits the events of the parent, and new events can be associated
with the new :class:`_engine.Engine` individually.
* The logging configuration and logging_name is copied from the parent
:class:`_engine.Engine`.
The intent of the :meth:`_engine.Engine.execution_options` method is
to implement schemes where multiple :class:`_engine.Engine`
objects refer to the same connection pool, but are differentiated
by options that affect some execution-level behavior for each
engine. One such example is breaking into separate "reader" and
"writer" :class:`_engine.Engine` instances, where one
:class:`_engine.Engine`
has a lower :term:`isolation level` setting configured or is even
transaction-disabled using "autocommit". An example of this
configuration is at :ref:`dbapi_autocommit_multiple`.
Another example is one that
uses a custom option ``shard_id`` which is consumed by an event
to change the current schema on a database connection::
from sqlalchemy import event
from sqlalchemy.engine import Engine
primary_engine = create_engine("mysql+mysqldb://")
shard1 = primary_engine.execution_options(shard_id="shard1")
shard2 = primary_engine.execution_options(shard_id="shard2")
shards = {"default": "base", "shard_1": "db1", "shard_2": "db2"}
@event.listens_for(Engine, "before_cursor_execute")
def _switch_shard(conn, cursor, stmt, params, context, executemany):
shard_id = conn.get_execution_options().get("shard_id", "default")
current_shard = conn.info.get("current_shard", None)
if current_shard != shard_id:
cursor.execute("use %s" % shards[shard_id])
conn.info["current_shard"] = shard_id
The above recipe illustrates two :class:`_engine.Engine` objects that
will each serve as factories for :class:`_engine.Connection` objects
that have pre-established "shard_id" execution options present. A
:meth:`_events.ConnectionEvents.before_cursor_execute` event handler
then interprets this execution option to emit a MySQL ``use`` statement
to switch databases before a statement execution, while at the same
time keeping track of which database we've established using the
:attr:`_engine.Connection.info` dictionary.
.. seealso::
:meth:`_engine.Connection.execution_options`
- update execution options
on a :class:`_engine.Connection` object.
:meth:`_engine.Engine.update_execution_options`
- update the execution
options for a given :class:`_engine.Engine` in place.
:meth:`_engine.Engine.get_execution_options`
Améliorations / Corrections
Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Emplacement :
Description des améliorations :