Context manager exit - automatically rollback if not committed If an exception occurred, always rollback. If no exception, rollback unless commit() was called.
(self, exc_type, exc_val, exc_tb)
| 178 | return self |
| 179 | |
| 180 | def __exit__(self, exc_type, exc_val, exc_tb): |
| 181 | """ |
| 182 | Context manager exit - automatically rollback if not committed |
| 183 | |
| 184 | If an exception occurred, always rollback. |
| 185 | If no exception, rollback unless commit() was called. |
| 186 | """ |
| 187 | if exc_type is not None: |
| 188 | # Exception occurred, always rollback |
| 189 | if not self._committed and not self._rolled_back: |
| 190 | try: |
| 191 | self.rollback() |
| 192 | except Exception: |
| 193 | pass # Ignore rollback errors during exception handling |
| 194 | else: |
| 195 | # No exception - rollback if not committed |
| 196 | if not self._committed and not self._rolled_back: |
| 197 | try: |
| 198 | self.rollback() |
| 199 | except Exception: |
| 200 | pass # Ignore rollback errors |
| 201 | |
| 202 | return False # Don't suppress exceptions |
| 203 | |
| 204 | |
| 205 | __all__ = ['Transaction'] |