0

I have a model and in which i use establish_connection to connect to different database(let name it remote database) of other application.

example:

class User < ActiveRecord::Base

  establish_connection(some_database_credential_hash)
  self.class_name = "members"

end

and in production environment i am running a rake task which uses User class in it.My question is that if the same rake task runs every hour, will it try to make new connection every time to new members table of remote database increasing load and pool on that database? if Yes, how can i avoid it? Please suggest.

Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
  • Anything in the body of a class will run on environment initialisation. When your rake task is called it will establish a connection, which will then be released when it finishes and shuts down the environment. It should not accumulate unless your task is rudely killed at some point (e.g. with `kill -9`) – AJFaraday Aug 06 '15 at 08:11
  • @AJFaraday you mean to say that connection established will no longer exist after rake task execution is completed, right? – Sachin Singh Aug 06 '15 at 08:13
  • Yes, I'm pretty certain of that. it should be possible to confirm it if you run your task a few times and then check the active connections on `remote database` – AJFaraday Aug 06 '15 at 08:23
  • @AJFaraday can you tell me how to check active connection on a database? – Sachin Singh Aug 06 '15 at 08:25
  • What type of database is it? Oracle? Postgresql? Mysql? – AJFaraday Aug 06 '15 at 08:31
  • It looks like you can log in to mysql as admin and run `show processlist`. http://stackoverflow.com/questions/7432241/mysql-show-status-active-or-total-connections – AJFaraday Aug 06 '15 at 08:56

1 Answers1

0

This was answered in comments:

  • Anything in the body of a class is run at the point of initialising the environment, which is done fresh each time a rake task is run.
  • Part of tearing down the rails environment is to close any open connections to databases.
  • Unless the rake task is rudely quit the connection established will be closed.
  • This can be confirmed by logging in to (in this case) mysql and running

    show processlist

AJFaraday
  • 2,411
  • 1
  • 16
  • 39