Search code examples
pythonpython-3.xrethinkdbrethinkdb-python

Rethinkdb: "Connection closed"


I'm using rethinkdb with flask (python 3) that is hosted locally on a Windows machine. The problem is that connection is being closed very frequently.

The error looks like this:

rethinkdb.errors.ReqlDriverError: Connection is closed.

In python i defined connection like this:

conn = r.connect("localhost", 28015).repl()

Example of my query:

  r.db('izmjene').table("users").get_all(
         email, index="email").count().run(conn)

Solution

  • The repl command is a convenience method that sets a default connection in your shell so you don’t have to pass it to the run command to run your queries. Avoid using repl in application code. RethinkDB connection objects are not thread-safe, and calls to connect from multiple threads may change the global connection object used by repl. Applications should specify connections explicitly.

    There is a very good example that demonstrates connecting and closing the connection here