Context manager that ensures that a connection is established, and if it opened one, closes it to avoid leaving a dangling connection. This is useful for operations outside of the request-response cycle. Provide a cursor: with self.temporary_connection() as cursor:
(self)
| 683 | |
| 684 | @contextmanager |
| 685 | def temporary_connection(self): |
| 686 | """ |
| 687 | Context manager that ensures that a connection is established, and |
| 688 | if it opened one, closes it to avoid leaving a dangling connection. |
| 689 | This is useful for operations outside of the request-response cycle. |
| 690 | |
| 691 | Provide a cursor: with self.temporary_connection() as cursor: ... |
| 692 | """ |
| 693 | must_close = self.connection is None |
| 694 | try: |
| 695 | with self.cursor() as cursor: |
| 696 | yield cursor |
| 697 | finally: |
| 698 | if must_close: |
| 699 | self.close() |
| 700 | |
| 701 | @contextmanager |
| 702 | def _nodb_cursor(self): |