diff --git a/lib/pgtk/pool.rb b/lib/pgtk/pool.rb index c40ee28..01d309f 100644 --- a/lib/pgtk/pool.rb +++ b/lib/pgtk/pool.rb @@ -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 diff --git a/test/test_pool.rb b/test/test_pool.rb index 01f29c1..76922c0 100644 --- a/test/test_pool.rb +++ b/test/test_pool.rb @@ -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