GC on WIN32OLE

I’ve found the following resource use pattern helpful when playing with ADO

  • stops me worrying about when to open or close a connection, and stops me
    worrying about the TOO MANY CONCURRENT CONNECTION error that used to plague
    me working with (spit) Access:

class SomeClassUsingADO
def initialize(…)
@sql_blocks_open = 0
@conn = WIN32OLE.new(“ADODB.Connection”)
@conn.ConnectionString = “dsn=…”
end

def _sql(&block)
@conn.Open if @sql_blocks_open == 0
@sql_blocks_open += 1
yield
ensure
@sql_blocks_open -= 1
@conn.Close if @sql_blocks_open == 0
end

#usage is good for nested operations - opening a
#connection in ADO is an expensive operation
def do_something_with_a_query(*args)
_sql do
a = do_something_else(*args)
@conn.Execute(“delete … where something=#{a}”)
end
end

def do_something_else(*args)
_sql do #preserves the same connection as above
rs = @conn.Execute(“select something”)
return rs.Fields.Item(“something”)
end
end
end

David Naseby

···

-----Original Message-----
From: Shannon Fang [mailto:xrfang@hotmail.com]
While reading the discussion on Garbage Collection, I
suddenly thought of
one question: for the following code

conn=WIN32OLE.new(“ADODB.Connection”)
conn.open(…)

do I need to manually call conn.Close, or ruby will take care
of that for
me?