Execute a function with auto-commit wrapping if needed
(&self, f: F)
| 581 | |
| 582 | /// Execute a function with auto-commit wrapping if needed |
| 583 | pub fn execute_with_auto_commit<F, R>(&self, f: F) -> Result<R, ExecutionError> |
| 584 | where |
| 585 | F: FnOnce() -> Result<R, ExecutionError>, |
| 586 | { |
| 587 | let needs_auto_commit = self.is_auto_commit()? && !self.has_active_transaction()?; |
| 588 | |
| 589 | if needs_auto_commit { |
| 590 | // Start auto-commit transaction |
| 591 | let txn_id = self.begin_transaction()?; |
| 592 | |
| 593 | // Execute the function |
| 594 | match f() { |
| 595 | Ok(result) => { |
| 596 | // Commit on success |
| 597 | self.commit_transaction()?; |
| 598 | Ok(result) |
| 599 | } |
| 600 | Err(e) => { |
| 601 | // Rollback on error |
| 602 | if let Err(rollback_err) = self.rollback_transaction() { |
| 603 | log::error!( |
| 604 | "Failed to rollback transaction {}: {}", |
| 605 | txn_id, |
| 606 | rollback_err |
| 607 | ); |
| 608 | } |
| 609 | Err(e) |
| 610 | } |
| 611 | } |
| 612 | } else { |
| 613 | // No auto-commit needed, just execute |
| 614 | f() |
| 615 | } |
| 616 | } |
| 617 | } |
nothing calls this directly
no test coverage detected