Rails mongoDB single connection required

144 views Asked by At

I am using rails application, my rails applications logs are should be stroed in mongoDB.

I am logging each and every controller's method call and its params its date of call etc.

Here is my code in my application controller, to log the information

db = Mongo::ReplSetConnection.new([MONGODB_PROP['host'],MONGODB_PROP['port']],:refresh_mode => :sync).db(MONGODB_PROP['database'])
au = db.authenticate(MONGODB_PROP['username'],MONGODB_PROP['password'])

if au
  coll = db.collection("log_info")
  doc = { :tab_name => "#{params[:controller}",:date =>"#{Time.now}"}
  coll.insert(doc)
end

Obviously, my code has need some standard issues. From my implementation each time the method called happend the mongoDB connection is established .So automatically connection object is increased & it will become performance issue. i want the Single DB connection whenever it requires i need to get the connection object and perform the insert operation. How can i do this.

Please help me on this.

1

There are 1 answers

0
Jesse Wolgamott On

The easiest way is to use Mongoid and create a LogInfo class. Let mongoid handle your database connections, and you simply call:

LogInfo.create(:tab_name => "#{params[:controller}",:date =>"#{Time.now}")