Skip to content

Commit

Permalink
push #89 rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 12, 2024
1 parent 4b5e498 commit 1bd49b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/pgtk/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,14 @@ def transaction
connect do |c|
t = Txn.new(c, @log)
t.exec('START TRANSACTION')
r = yield t
t.exec('COMMIT')
r
begin
r = yield t
t.exec('COMMIT')
r
rescue StandardError => e
t.exec('ROLLBACK')
raise e
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/test_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def test_transaction_with_error
end
end
assert(pool.exec('SELECT * FROM book').empty?)
pool.exec('INSERT INTO book (title) VALUES ($1)', ['another'])
assert(!pool.exec('SELECT * FROM book').empty?)
end
end

Expand Down

0 comments on commit 1bd49b2

Please sign in to comment.