| | 626 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| | 627 | ;;;; Transaction management |
| | 628 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| | 629 | |
| | 630 | (define (with-transaction conn thunk) |
| | 631 | (let* ((old-depth (pg-connection-transaction-depth conn)) |
| | 632 | (rollback! |
| | 633 | (lambda () |
| | 634 | (if (= old-depth 0) |
| | 635 | (query conn "ROLLBACK") |
| | 636 | ;; We do not *need* to give savepoints unique names, |
| | 637 | ;; but it aids debugging and we know the depth anyway. |
| | 638 | (query conn (conc "ROLLBACK TO SAVEPOINT s_" old-depth))))) |
| | 639 | (commit! |
| | 640 | (lambda () |
| | 641 | (if (= old-depth 0) |
| | 642 | (query conn "COMMIT") |
| | 643 | (query conn (conc "RELEASE SAVEPOINT s_" old-depth)))))) |
| | 644 | (if (= old-depth 0) |
| | 645 | (query conn "BEGIN") |
| | 646 | (query conn (conc "SAVEPOINT s_" old-depth))) |
| | 647 | (pg-connection-transaction-depth-set! conn (add1 old-depth)) |
| | 648 | ;; TODO: Add a warning mechanism (using dynamic-wind) for when the |
| | 649 | ;; user tries to jump into/out of transactions with continuations? |
| | 650 | (handle-exceptions exn |
| | 651 | (begin |
| | 652 | (pg-connection-transaction-depth-set! conn old-depth) |
| | 653 | (rollback!) |
| | 654 | (raise exn)) |
| | 655 | (let ((res (thunk))) |
| | 656 | (pg-connection-transaction-depth-set! conn old-depth) |
| | 657 | (if res (commit!) (rollback!)) |
| | 658 | res)))) |
| | 659 | |
| | 660 | (define (in-transaction? conn) |
| | 661 | (> (pg-connection-transaction-depth conn) 0)) |
| | 662 | |