From: Tom Sawyer Date: 2003-01-26T13:18:59+09:00 Subject: Re: mod_ruby: what's persistent and what's shared? On Saturday 25 January 2003 08:12 pm, ahoward wrote: > have you had any issues sharing one connection between many processes? i > mean what if : > 1) process a : > $db.exec 'connect foobar_database' > 2) process b : > $db.exec 'connect barfoo_database' > 3) process a : > $db.exec # something which has something to do with foobar_database, > but # it's connected to barfoo_database!! i get around this by storing the connections in a hash based on connection info. so connections to different dbs/users get their own connection in the pool. here's the code: module DBI # singleton instance # provides simple pooling of database connections # note: the pools are defined by dsn and username; # thus optional parameters are expected to be the same for these def DBI.instance(dsn, user, pass, *args) @@dbi = {} unless defined?(@@dbi) @@dbi["#{dsn}::#{user}"] ||= DBI.connect(dsn, user, pass, *args) return @@dbi["#{dsn}::#{user}"] end end > > it runs as a seperate service (using DRb)? > > yes. it's a totall hack right now, but the _idea_ is a good one i think. > making it totally generic would be tough, but the general idea would be to > place a naming service in a rinda space or simply a drb object which you > could look up other objects by. the rinda space would allow you to leave > object behind between cgi invocations (with all the problems that entails). > what i'm really looking into now is how to efficiently multiplex and bunch > of database connections, eg how to design a connection pool. anyone out > there done this? very cool. sorry i don't really have any expertise in this to help. the above DBI.instance is my extent. let me know how it goes though. > > i wonder then. if i use DRb would i then also be able to make my app > > easily run on a distributed enviornment? i.e. a cluster? > > yeah. that's a definitely possibility and one which i've considered. see. now that would be the best reason to use it, i think. -- tom sawyer, aka transami transami@transami.net