ccurtisj
ccurtisj

Reputation: 398

How to reset a mongoid 3 database connection

In Mongoid 2.x one could execute Mongoid.database.connection.close to reset a database connection. This specific API is no longer available in Mongoid3, what is the new way to reset a connection?

Motivation: interrupting a query while it's in-progress (ctrl-c) will kill your connection. Any subsequent queries will just hang. Additionally, thrown errors such as timeouts will do the same thing (see: http://simple10.com/rescuing-from-racktimeout-to-close-mongodb-connection).

Upvotes: 3

Views: 1064

Answers (3)

Bruno Tavares
Bruno Tavares

Reputation: 1

Besides disconnecting a specific session with

Mongoid::Sessions.default.disconnect

You can also disconnect all sessions with

Mongoid::Sessions.disconnect

You can get a list of all active sessions with

Mongoid.sessions

Upvotes: 0

davogones
davogones

Reputation: 7399

I found the solution!

Mongoid::Sessions.default.disconnect

or

Model.collection.database.session.disconnect

This will properly reset the connection if you ctrl-c to interrupt a query in the console.

Upvotes: 4

ccurtisj
ccurtisj

Reputation: 398

It turns out that Mongoid3's replacement mongo driver, Moped, actually handles retrying connections automatically.

https://github.com/mongoid/moped/blob/master/lib/moped/node.rb#L115

Upvotes: 0

Related Questions