r"""Return a :class:`_engine.Connection` object corresponding to this :class:`.Session` object's transactional state. Either the :class:`_engine.Connection` corresponding to the current transaction is returned, or if no transaction is in progress, a new one is begun
(
self,
bind_arguments: Optional[_BindArguments] = None,
execution_options: Optional[CoreExecuteOptionsParameter] = None,
)
| 2052 | trans.prepare() |
| 2053 | |
| 2054 | def connection( |
| 2055 | self, |
| 2056 | bind_arguments: Optional[_BindArguments] = None, |
| 2057 | execution_options: Optional[CoreExecuteOptionsParameter] = None, |
| 2058 | ) -> Connection: |
| 2059 | r"""Return a :class:`_engine.Connection` object corresponding to this |
| 2060 | :class:`.Session` object's transactional state. |
| 2061 | |
| 2062 | Either the :class:`_engine.Connection` corresponding to the current |
| 2063 | transaction is returned, or if no transaction is in progress, a new |
| 2064 | one is begun and the :class:`_engine.Connection` |
| 2065 | returned (note that no |
| 2066 | transactional state is established with the DBAPI until the first |
| 2067 | SQL statement is emitted). |
| 2068 | |
| 2069 | Ambiguity in multi-bind or unbound :class:`.Session` objects can be |
| 2070 | resolved through any of the optional keyword arguments. This |
| 2071 | ultimately makes usage of the :meth:`.get_bind` method for resolution. |
| 2072 | |
| 2073 | :param bind_arguments: dictionary of bind arguments. May include |
| 2074 | "mapper", "bind", "clause", other custom arguments that are passed |
| 2075 | to :meth:`.Session.get_bind`. |
| 2076 | |
| 2077 | :param execution_options: a dictionary of execution options that will |
| 2078 | be passed to :meth:`_engine.Connection.execution_options`, **when the |
| 2079 | connection is first procured only**. If the connection is already |
| 2080 | present within the :class:`.Session`, a warning is emitted and |
| 2081 | the arguments are ignored. |
| 2082 | |
| 2083 | .. seealso:: |
| 2084 | |
| 2085 | :ref:`session_transaction_isolation` |
| 2086 | |
| 2087 | """ |
| 2088 | |
| 2089 | if bind_arguments: |
| 2090 | bind = bind_arguments.pop("bind", None) |
| 2091 | |
| 2092 | if bind is None: |
| 2093 | bind = self.get_bind(**bind_arguments) |
| 2094 | else: |
| 2095 | bind = self.get_bind() |
| 2096 | |
| 2097 | return self._connection_for_bind( |
| 2098 | bind, |
| 2099 | execution_options=execution_options, |
| 2100 | ) |
| 2101 | |
| 2102 | def _connection_for_bind( |
| 2103 | self, |