Skip to content

Commit

Permalink
Merge pull request afair#58 from pedrofcuba/exception-in-failed-txn
Browse files Browse the repository at this point in the history
Skip closing cursor if connection held is no longer active
  • Loading branch information
afair committed Oct 29, 2022
2 parents 6123a23 + f5a6b1f commit e835ed6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/postgresql_cursor/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def each_tuple(&block) #:nodoc:
rescue Exception => e
raise e
ensure
close if @block
close if @block && connection.active?
end
end
@count
Expand All @@ -216,7 +216,7 @@ def each_batch(&block) #:nodoc:
break if has_do_while && rc != @options[:while]
end
ensure
close if @block
close if @block && connection.active?
end
end
@count
Expand Down
20 changes: 20 additions & 0 deletions test/test_postgresql_cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ def test_batch_exception
assert_equal e.message, 'Oops'
end

def test_exception_in_failed_transaction
begin
Product.each_row_by_sql("select * from products") do |r|
Product.connection.execute('select kaboom')
end
rescue Exception => e
assert_match(/kaboom/, e.message)
end
end

def test_batch_exception_in_failed_transaction
begin
Product.each_row_batch_by_sql("select * from products") do |r|
Product.connection.execute('select kaboom')
end
rescue Exception => e
assert_match(/kaboom/, e.message)
end
end

def test_cursor
cursor = Product.all.each_row
assert cursor.respond_to?(:each)
Expand Down

0 comments on commit e835ed6

Please sign in to comment.