Too Many Mysql connections

In my app, for three models: foo,bar and baz, the database connection
is made to a different database and hence in model code:

class Foo < ActiveRecord::Base
  establish_connection :diff_database
end

class Bar < ActiveRecord::Base
  establish_connection :diff_database
end

class Baz < ActiveRecord::Base
  establish_connection :diff_database
end

Now, the problem is, if is use the above code, not one but three
connections to another database is made and it seems that number of
connections made to the database keeps rising.

The mysql forks a new process for handling each new connection and
following shell command reports a rising number of forked processes.

ps ax|grep mysql|egrep 'local'|wc -l

And eventually when number of forked processes touches > 35, then i
get error, Mysql::Error too many connections open.

Now, I must use these three tables from old database because of some
datab constraints, so what are my options here?

Any pointers would be appreciated.

class LegacyDB < ActiveRecord::Base
  self.abstract_class = true # this prevents ActiveRecord from trying to find a table called "legacy_d_b" or the like
  establish_connection :diff_database
end

class Foo < LegacyDB
end

class Bar < LegacyDB
end

class Baz < LegacyDB
end

hemant wrote:

Thanks man.