mongoidmongoid3moped

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


Solution

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