Skip to content
Closed
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
10 changes: 6 additions & 4 deletions lib/plsql/oci_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ def exec(sql, *bindvars)
class Cursor #:nodoc:
include Connection::CursorCommon

# stack of open cursors
@@open_cursors = []
# stacks of open cursors (for each thread)
def self.open_cursors(thread_id = Thread.current.object_id)
(@@open_cursors ||= Hash.new {|stacks, id| stacks[id] = []})[thread_id]
end
attr_reader :raw_cursor

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 +127,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
9 changes: 9 additions & 0 deletions spec/plsql/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ class TestModel < TestBaseModel
plsql.activerecord_class = TestModel
plsql.schema_name.should == 'HR'
end

it "should safely close cursors in threaded environment" do
unless defined?(JRuby)
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.join
end
end

end if defined?(ActiveRecord)

describe "DBMS_OUTPUT logging" do
Expand Down