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