Sachin Singh
Sachin Singh

Reputation: 7225

issue with establish_connection and rake task together

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.

Upvotes: 0

Views: 448

Answers (1)

AJFaraday
AJFaraday

Reputation: 2450

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

Upvotes: 0

Related Questions