Skip to content

Instantly share code, notes, and snippets.

@nanodeath
Created December 13, 2009 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nanodeath/255174 to your computer and use it in GitHub Desktop.
Save nanodeath/255174 to your computer and use it in GitHub Desktop.
module Destroyable
def call_destructor(id)
@destructors[id].each do |o|
o.__destroy(id) if o.respond_to? :__destroy
end
@destructors.delete(id)
end
def is_destroyable
@destructors = {}
include InstanceMethods
end
def set_resources(id, res)
@destructors[id] = res
end
module InstanceMethods
def initialize
ObjectSpace.define_finalizer(self, self.class.method(:call_destructor).to_proc)
end
def declare_resources(res)
self.class.set_resources(object_id, res)
end
end
end
class MyBaseClass
extend Destroyable
end
class MyClass < MyBaseClass
is_destroyable
def initialize
super
puts "#{object_id}: MyClass created"
@db = DBConnection.new("jdbc://foo@bar/")
declare_resources([@db])
end
def access_db
@db.do_stuff
end
end
class DBConnection
def initialize(conn_string)
@conn_string = conn_string
@connected = false
end
def connect
puts "Connecting to #{@conn_string}"
@db = "Foo" + rand.to_s
@connected = true
end
def do_stuff
connect unless @connected
puts "Doing stuff with #{@conn_string}"
end
def query_active?
return rand < 0.5
end
def __destroy(id)
puts " #{id}: Closing #{@conn_string}."
if query_active?
puts " #{id}: Just kidding, can't close while query is running."
elsif !@connected
puts " #{id}: Just kidding, DB connection already closed."
else
puts " #{id}: Connection closed."
@connected = false
end
end
end
# test code
3.times {
MyClass.new.access_db
}
ObjectSpace.garbage_collect
puts "done"
6: MyClass created
Connecting to jdbc://foo@bar/
Doing stuff with jdbc://foo@bar/
8: MyClass created
Connecting to jdbc://foo@bar/
Doing stuff with jdbc://foo@bar/
10: MyClass created
Connecting to jdbc://foo@bar/
Doing stuff with jdbc://foo@bar/
done
10: Closing jdbc://foo@bar/.
10: Just kidding, can't close while query is running.
8: Closing jdbc://foo@bar/.
8: Connection closed.
6: Closing jdbc://foo@bar/.
6: Connection closed.
-608142678: MyClass created
Connecting to jdbc://foo@bar/
Doing stuff with jdbc://foo@bar/
-608142818: MyClass created
Connecting to jdbc://foo@bar/
Doing stuff with jdbc://foo@bar/
-608143018: MyClass created
Connecting to jdbc://foo@bar/
Doing stuff with jdbc://foo@bar/
-608142678: Closing jdbc://foo@bar/.
-608142678: Connection closed.
-608142818: Closing jdbc://foo@bar/.
-608142818: Connection closed.
-608143018: Closing jdbc://foo@bar/.
-608143018: Just kidding, can't close while query is running.
done
73998480: MyClass created
Connecting to jdbc://foo@bar/
Doing stuff with jdbc://foo@bar/
73998290: MyClass created
Connecting to jdbc://foo@bar/
Doing stuff with jdbc://foo@bar/
73998100: MyClass created
Connecting to jdbc://foo@bar/
Doing stuff with jdbc://foo@bar/
73998480: Closing jdbc://foo@bar/.
73998480: Just kidding, can't close while query is running.
73998290: Closing jdbc://foo@bar/.
73998290: Connection closed.
done
73998100: Closing jdbc://foo@bar/.
73998100: Just kidding, can't close while query is running.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment