tobinbradley/dirt-simple-postgis-http-api

warning Creating a duplicate database object for the same connection.

Closed this issue · 8 comments

i hit this warning in the console once a request is made for a mvt,
warning Creating a duplicate database object for the same connection
the tiles are getting delivered in one response not twice but once you set up an onclick event that returns feature properties, it returns 2 objects in the array for the same layer, so any clue on that ?
also i have a question, i noticed that you didn't put the .pbf extension on the tiles path and it works fine returning the tiles but just a thought why not adding it ?

There's too much going on there for me to hazard a guess. Could be the data, the JS client, the client side code, the server, or some combination of those.

As to why no pbf extension, it isn't necessary, and if you send a request for an area with no data, you won't get a pbf back, you'll get a 404 (that's how MapBox handles no data tiles, so I did the same).

well i found out that the issue is related to the (pg-promise ), what is causing this warning is that you set the connection for the database each time a request made for a service by declaring the db variable inside the handler function like that

handler: function(request, reply) { let db = pgp(config.db.postgis); db .query(formatSQL(request)) .then(function(data) { reply(data); }) .catch(function(err) { reply({ 'error': 'error running query', 'error_details': err }); }); }
based on the question here
so you need to declare the let db = pgp(config.db.postgis); outside each route file and then import it instead of re-declaring it this will solve it and avoid re-creating of the database connection with every tile requested, but note that in the handler function now should be something like this
let db = require('your module path')
handler: function(request, reply) { let db = pgp(config.db.postgis); db.db #in case you named your variable db in the external module .query(formatSQL(request)) .then(function(data) { reply(data); }) .catch(function(err) { reply({ 'error': 'error running query', 'error_details': err }); }); }
i don't know how would this effect performance or not but here it's anyway.

Awesome, thanks! From your original comment I thought you saw that message in the browser console. I'll check it out.

oh sorry for confusion, just to inform you that this warning has nothing to do with the double return for the properties object i mentioned earlier " that was my fault ", and also as i said i'm not sure if it cause any performance issues but at least you won't have a server logs full of this message, i just deployed to heroku and tested in maputink, everything works like a charm.

No worries, thanks for researching the warning message!

The warning comes from pg-promise, and the explanation can be found here: http://stackoverflow.com/questions/34382796/where-should-i-initialize-pg-promise

And if you try creating more than one database object with the same connection details, the library will output a warning into the console:

WARNING: Creating a duplicate database object for the same connection.
at Object. (D:\NodeJS\tests\test2.js:14:6)

This points out that your database usage pattern is bad, i.e. you should share the database object, as shown above, not re-create it all over again.

Thank you vitaly

you should share the database object, as shown above, not re-create it all over again.

I have a file with your Robust Listener code in it
I also have another file with your db initialization code in it
Should both be the way they are or should I share, its a permanent listener as I am getting this warning