Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/plsql/oci_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@ def exec(sql, *bindvars)
class Cursor #:nodoc:
include Connection::CursorCommon

# stack of open cursors
@@open_cursors = []
attr_reader :raw_cursor

# stack of open cursors per thread
def self.open_cursors
Thread.current[:plsql_oci_cursor_stack] ||= []
end

def initialize(conn, raw_cursor)
@connection = conn
@raw_cursor = raw_cursor
@@open_cursors.push self
self.class.open_cursors.push self
end

def self.new_from_parse(conn, sql)
Expand Down Expand Up @@ -125,7 +128,7 @@ def close_raw_cursor

def close
# close all cursors that were created after this one
while (open_cursor = @@open_cursors.pop) && !open_cursor.equal?(self)
while (open_cursor = self.class.open_cursors.pop) && !open_cursor.equal?(self)
open_cursor.close_raw_cursor
end
close_raw_cursor
Expand Down Expand Up @@ -336,4 +339,4 @@ def ora_date_to_ruby_date(val)

end

end
end
9 changes: 9 additions & 0 deletions spec/plsql/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ class TestModel < TestBaseModel
plsql.activerecord_class = TestModel
expect(plsql.schema_name).to eq('HR')
end

it "should safely close cursors in threaded environment" do
expect {
t1 = Thread.new { plsql.dbms_lock.sleep(1) }.tap { |t| t.abort_on_exception = true }
t2 = Thread.new { plsql.dbms_lock.sleep(2) }.tap { |t| t.abort_on_exception = true }
[t2, t1].each { |t| t.join }
}.not_to raise_error
end

end if defined?(ActiveRecord)

describe "DBMS_OUTPUT logging" do
Expand Down