| 268 | |
| 269 | #################### |
| 270 | # CONTEXT MANAGERS # |
| 271 | #################### |
| 272 | class Transaction(object): |
| 273 | def __enter__(cls): |
| 274 | enter_transaction_management() |
| 275 | managed(True) |
| 276 | __enter__ = classmethod(__enter__) |
| 277 | |
| 278 | def __exit__(cls, exc_type, value, traceback): |
| 279 | try: |
| 280 | if exc_type is None: |
| 281 | # Nested block executed successfully |
| 282 | if is_dirty(): |
| 283 | commit() |
| 284 | else: |
| 285 | # Some exception was raised |
| 286 | if is_dirty(): |
| 287 | rollback() |
| 288 | return False # re-raises the exception |
| 289 | finally: |
| 290 | leave_transaction_management() |
| 291 | __exit__ = classmethod(__exit__) |