Too many database connections created in rails 2.3.18 application

299 views Asked by At

We have many models like the below one.

class User < ActiveRecord::Base
 create_connection

  def create_connection  
    # we have used this ActiveRecord::Base.configurations[current_environment] for connection_info 
    self.establish_connection connection_info
  end
end 

After using many models of similar types, many database connections are being created for the remote database. So we are getting the below error message for remote Postgres database.

2013-12-04T12:18:28.360740+00:00 app[web.1]:   FATAL:  too many connections for role "ycjcwpowlrmvhi"

Actually we have two applications on heroku and each application accesses the other's database.

How we can close these many database connection after executing the query?

We are also getting the below error some times.

2013-12-06T09:36:27.952787+00:00 app[web.1]: Processing StaffController#index (for 202.45.6.95 at 2013-12-06 09:36:27) [GET]
2013-12-06T09:36:16.453533+00:00 heroku[router]: at=info method=GET path=/javascripts/application.js?1386322453 host=demomanager.herokuapp.com fwd="202.45.6.95" dyno=web.1 connect=1ms service=3ms status=200 bytes=5025
2013-12-06T09:36:32.935950+00:00 app[web.1]: 
2013-12-06T09:36:32.935950+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::UnableToSend: SSL error: decryption failed or bad record mac
2013-12-06T09:36:32.935950+00:00 app[web.1]: :             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
2013-12-06T09:36:32.935950+00:00 app[web.1]:               FROM pg_attribute a LEFT JOIN pg_attrdef d
2013-12-06T09:36:32.935950+00:00 app[web.1]:                 ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2013-12-06T09:36:32.935950+00:00 app[web.1]:              WHERE a.attrelid = '"tags"'::regclass
2013-12-06T09:36:32.935950+00:00 app[web.1]:                AND a.attnum > 0 AND NOT a.attisdropped
2013-12-06T09:36:32.935950+00:00 app[web.1]:              ORDER BY a.attnum
2013-12-06T09:36:32.935950+00:00 app[web.1]: ):
1

There are 1 answers

0
ckruse On

You should be able to change the number of database connections by changing the pool parameter in your database.yml and lowering that value.