Tobias Reich
Tobias Reich

Reputation: 2034

RethinkDB connection "must be open" but never gets closed

I'm using RethinkDB together with hapi.js in Node.js. My main application passes an open connection to a hapi.js plugin. Using the connection inside the main-script works fine, but running something inside the plugin throws the following error:

Unhandled rejection ReqlDriverError: First argument to `run` must be an open connection.

I tried to figure out if the connection gets closed, but non of the added listeners fire.

connection.addListener('connect', () => { console.log('!!connect') })
connection.addListener('close', () => { console.log('!!closed') })
connection.addListener('timeout', () => { console.log('!!timeout') })
connection.addListener('error', () => { console.log('!!error') })

I can confirm the the event listers are working in general as calling connection.close() manually outputs !!closed.

I asked the hapi.js community on GitHub if a passed object will be passed to plugins as a copy, but that's actually not the case.

Here's the code throwing the error:

console.log(server.root.app.connection) // {…} === connection
console.log(server.root.app.connection.open) // true

r.table('records').group('siteLocation').count().run(server.root.app.connection, (err, cursor) => {

    ...

})

To sum it up:

Is there any way to find out why the connection is not open anymore?

RethinkDB version: 2.1.5
hapi.js version: 11.1.2

Thanks!

Upvotes: 5

Views: 2605

Answers (1)

Tobias Reich
Tobias Reich

Reputation: 2034

Found the problem:

The provided error is misleading. It's not the connection, it's the rethinkdb module! I import/require it in the main application and inside plugins. It's the same module, but a different instance of it. The connection itself is the same and still open.

Passing the connection AND the rethinkdb instance to plugins works. Passing the connection to plugins and using their rethinkdb does not. The connection must be from the same instance as later used to get data from the database.

Upvotes: 5

Related Questions