(Please don''t suggest I rethink the table / database design; I am writing inquiry applications on existing ERP data models. They are what they are.) Let''s say I want to show the contents of table T1 from database DB1 and allow the user to look at the same table (T1) in database DB2, DB3, etc. via hyperlinks on the T1 / DB1 page. I have all my models, controllers, etc. working. I have it set up so that the controller and models figure out when the database needs to switch and establishes a connection with the correct database. The problem I am having is that I am exhausting the number of connections the database will allow when I do this switching in development mode (restarting the server seems to release all the discarded connections). Active::Record.connection will give you a connection, but you can''t give it back (reuse it), since the connection= method takes a connection parameter hash, not a connection. I can''t think of any simple work-around with model class hierarchy, etc. I''m thinking I need to implement my own connection pooling in the XXX_adapter.rb? Suggestions?
Steve Downey wrote:> (Please don''t suggest I rethink the table / database design; I am > writing inquiry applications on existing ERP data models. They are > what they are.) > > Let''s say I want to show the contents of table T1 from database DB1 > and allow the user to look at the same table (T1) in database DB2, > DB3, etc. via hyperlinks on the T1 / DB1 page. > > I have all my models, controllers, etc. working. I have it set up so > that the controller and models figure out when the database needs to > switch and establishes a connection with the correct database. > > The problem I am having is that I am exhausting the number of > connections the database will allow when I do this switching in > development mode (restarting the server seems to release all the > discarded connections). > > Active::Record.connection will give you a connection, but you can''t > give it back (reuse it), since the connection= method takes a > connection parameter hash, not a connection. > > I can''t think of any simple work-around with model class hierarchy, > etc. I''m thinking I need to implement my own connection pooling in > the XXX_adapter.rb? Suggestions? > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >The most elegant solution is likely a federated data source. What database are you running on? If it is DB2, you can use Information Integrator. If it is Oracle, you may be able to use DB Links. If it is anything else, you may be able to use SQLRelay.
On 8/10/05, Steve Downey <sldowney-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote:> Active::Record.connection will give you a connection, but you can''t give > it back (reuse it), since the connection= method takes a connection > parameter hash, not a connection.Why not just re-open the ActiveRecord::Base class and write a new #connection= method in such a way that it could take either a connection instance or the parameter hash? Just alias the old version first and have that one be called if the parameter hash is passed instead of a connection object. Ahh, the beauty of Ruby''s open classes... -- Regards, John Wilger ----------- Alice came to a fork in the road. "Which road do I take?" she asked. "Where do you want to go?" responded the Cheshire cat. "I don''t know," Alice answered. "Then," said the cat, "it doesn''t matter." - Lewis Carrol, Alice in Wonderland
John Wilger wrote:>On 8/10/05, Steve Downey <sldowney-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote: > >>Active::Record.connection will give you a connection, but you can''t give >>it back (reuse it), since the connection= method takes a connection >>parameter hash, not a connection. >> > >Why not just re-open the ActiveRecord::Base class and write a new >#connection= method in such a way that it could take either a >connection instance or the parameter hash? Just alias the old version >first and have that one be called if the parameter hash is passed >instead of a connection object. > >Ahh, the beauty of Ruby''s open classes... > >Since I already had sub-classed ActiveRecord::Base to collect some behaviors for my classes, I took the original "connect=" which looks like: def self.connection=(spec) raise ConnectionNotEstablished unless spec conn = self.send(spec.adapter_method, spec.config) active_connections[self] = conn end and mine now looks like: @@connection_cache = {} def self.connection=(spec) raise ConnectionNotEstablished unless spec db = spec.config[:database] cached_conn = @@connection_cache[db] if cached_conn active_connections[self] = cached_conn else conn = self.send(spec.adapter_method, spec.config) active_connections[self] = conn @@connection_cache[db] = conn end end which seems to work ... in production. But in development environment the @@connection_cache is always empty for each new request. May have to do the caching in the adapter which I don''t think gets reloaded in dev. Anyone see any problems with this?