3

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).

ccurtisj
  • 388
  • 2
  • 13

3 Answers3

4

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.

davogones
  • 7,321
  • 31
  • 36
0

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

ccurtisj
  • 388
  • 2
  • 13
0

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