From: Mike Bayer Date: Thu, 29 Apr 2021 12:44:30 +0000 (-0400) Subject: dont use context from other sections X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=611a81315eb51e0463146d7c8104d5196e956058;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git dont use context from other sections rewrite the "individual sessions" section to not rely upon context from the previous section, this is a bad habit and should be fixed whereever we can find it becuase people click on individual sections out of context. Change-Id: I4f09c5a582808eba3dbf9ba362e2af22edf2f2c7 --- diff --git a/doc/build/orm/session_transaction.rst b/doc/build/orm/session_transaction.rst index 5aaf4f2748..77536bd954 100644 --- a/doc/build/orm/session_transaction.rst +++ b/doc/build/orm/session_transaction.rst @@ -479,12 +479,19 @@ Setting Isolation for Individual Sessions When we make a new :class:`.Session`, either using the constructor directly or when we call upon the callable produced by a :class:`.sessionmaker`, we can pass the ``bind`` argument directly, overriding the pre-existing bind. -We can for example create our :class:`_orm.Session` from the -"``transactional_session``" and pass the "``autocommit_engine``":: +We can for example create our :class:`_orm.Session` from a default +:class:`.sessionmaker` and pass an engine set for autocommit:: - session = transactional_session(bind=autocommit_engine) - # work with session - session.close() + plain_engine = create_engine("postgresql://scott:tiger@localhost/test") + + autocommit_engine = eng.execution_options(isolation_level="AUTOCOMMIT") + + # will normally use plain_engine + Session = sessionmaker(plain_engine) + + # make a specific Session that will use the "autocommit" engine + with Session(bind=autocommit_engine) as session: + # work with session For the case where the :class:`.Session` or :class:`.sessionmaker` is configured with multiple "binds", we can either re-specify the ``binds`` @@ -492,10 +499,8 @@ argument fully, or if we want to only replace specific binds, we can use the :meth:`.Session.bind_mapper` or :meth:`.Session.bind_table` methods:: - session = maker() - session.bind_mapper(User, autocommit_engine) - -We can also use the individual transaction method that follows. + with Session() as session: + session.bind_mapper(User, autocommit_engine) Setting Isolation for Individual Transactions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~