| 1783 | |
| 1784 | @contextlib.contextmanager |
| 1785 | def _checkout( |
| 1786 | self, server: Server, session: Optional[ClientSession] |
| 1787 | ) -> Generator[Connection, None]: |
| 1788 | in_txn = session and session.in_transaction |
| 1789 | with _MongoClientErrorHandler(self, server, session) as err_handler: |
| 1790 | # Reuse the pinned connection, if it exists. |
| 1791 | if in_txn and session and session._pinned_connection: |
| 1792 | err_handler.contribute_socket(session._pinned_connection) |
| 1793 | yield session._pinned_connection |
| 1794 | return |
| 1795 | with server.checkout(handler=err_handler) as conn: |
| 1796 | # Pin this session to the selected server or connection. |
| 1797 | if ( |
| 1798 | in_txn |
| 1799 | and session |
| 1800 | and server.description.server_type |
| 1801 | in ( |
| 1802 | SERVER_TYPE.Mongos, |
| 1803 | SERVER_TYPE.LoadBalancer, |
| 1804 | ) |
| 1805 | ): |
| 1806 | session._pin(server, conn) |
| 1807 | err_handler.contribute_socket(conn) |
| 1808 | if ( |
| 1809 | self._encrypter |
| 1810 | and not self._encrypter._bypass_auto_encryption |
| 1811 | and conn.max_wire_version < 8 |
| 1812 | ): |
| 1813 | raise ConfigurationError( |
| 1814 | "Auto-encryption requires a minimum MongoDB version of 4.2" |
| 1815 | ) |
| 1816 | yield conn |
| 1817 | |
| 1818 | def _select_server( |
| 1819 | self, |