Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f5a6b1f

Browse files
Skip closing cursor if connection held is no longer active
It avoids issues when a transaction fails, thus the statement to close the cursor will fail. In addition this obscures the underlying error in the user code due to raising when closing the cursor.
1 parent 6123a23 commit f5a6b1f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

‎lib/postgresql_cursor/cursor.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def each_tuple(&block) #:nodoc:
194194
rescue Exception => e
195195
raise e
196196
ensure
197-
close if @block
197+
close if @block && connection.active?
198198
end
199199
end
200200
@count
@@ -216,7 +216,7 @@ def each_batch(&block) #:nodoc:
216216
break if has_do_while && rc != @options[:while]
217217
end
218218
ensure
219-
close if @block
219+
close if @block && connection.active?
220220
end
221221
end
222222
@count

‎test/test_postgresql_cursor.rb‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,26 @@ def test_batch_exception
130130
assert_equal e.message, 'Oops'
131131
end
132132

133+
def test_exception_in_failed_transaction
134+
begin
135+
Product.each_row_by_sql("select * from products") do |r|
136+
Product.connection.execute('select kaboom')
137+
end
138+
rescue Exception => e
139+
assert_match(/kaboom/, e.message)
140+
end
141+
end
142+
143+
def test_batch_exception_in_failed_transaction
144+
begin
145+
Product.each_row_batch_by_sql("select * from products") do |r|
146+
Product.connection.execute('select kaboom')
147+
end
148+
rescue Exception => e
149+
assert_match(/kaboom/, e.message)
150+
end
151+
end
152+
133153
def test_cursor
134154
cursor = Product.all.each_row
135155
assert cursor.respond_to?(:each)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /