brianc / node-postgres

PostgreSQL client for node.js.
https://node-postgres.com
MIT License
12.37k stars 1.23k forks source link

pg dies after 10 connections to the DDBB #351

Closed fox-alvarez closed 11 years ago

fox-alvarez commented 11 years ago

Hello

after 10 connections to the database the "pg" dies and the node is still alive but can not interact with the DDBB.

The following is the code of my servidor.js

var pg = require('pg');
pg.DEBUG = 1;
var conString = "myConnectionString";
var cvanderito = require("socket.io").listen(6969);

cvanderito.configure(function(){
    cvanderito.set('log level', 1);
    cvanderito.set('transports', ['websocket', 'flashsocket', 'htmlfile', 'jsonp-polling', 'xhr-polling' ]);     
});

cvanderito.sockets.on("connection",arranque);

function arranque(usuario){
   usuario.on("nuevoNombre",emitir);    
}
function emitir(data){
    console.log("hello");

    pg.connect(conString, function(err,client) {
        var soc=Math.floor((Math.random()*1000)+1);
        var StringConsulta="select conectados_ins(1,'"+soc+"',true,2)"; 
        var consultaSQL=client.query(StringConsulta);
        consultaSQL.on('row', function(row) {
            client.end.bind(client);
            console.log("User successfully added");
        });
    });
    cvanderito.sockets.emit("nombreDesdeServidor",data+"*");   
}

node version:

v0.8.21

output after installation of pg:

npm WARN cannot run in wd pg@1.1.0 rm -r build || (exit 0) (wd=pg)
npm http GET https://registry.npmjs.org/generic-pool/2.0.2
npm http GET https://registry.npmjs.org/buffer-writer/1.0.0
npm http 304 https://registry.npmjs.org/generic-pool/2.0.2
npm http 304 https://registry.npmjs.org/buffer-writer/1.0.0
npm WARN package.json buffer-writer@1.0.0 No README.md file found!
npm http GET https://registry.npmjs.org/cloned/0.0.1
npm http GET https://registry.npmjs.org/async
npm http 304 https://registry.npmjs.org/cloned/0.0.1
npm http 304 https://registry.npmjs.org/async
npm http GET https://registry.npmjs.org/rmdir
npm http 304 https://registry.npmjs.org/rmdir

> pg@1.1.0 install /root/nvm/v0.8.21/lib/node_modules/pg
> node-gyp rebuild || (exit 0)

make: Entering directory `/root/nvm/v0.8.21/lib/node_modules/pg/build'
  CXX(target) Release/obj.target/binding/src/binding.o
  SOLINK_MODULE(target) Release/obj.target/binding.node
  SOLINK_MODULE(target) Release/obj.target/binding.node: Finished
  COPY Release/binding.node
make: Leaving directory `/root/nvm/v0.8.21/lib/node_modules/pg/build'
pg@1.1.0 /root/nvm/v0.8.21/lib/node_modules/pg
âââ generic-pool@2.0.2
âââ buffer-writer@1.0.0 (async@0.2.8, cloned@0.0.1)

I'm sure it's on the 10th connexion that "pg" dies, because the console does not return to apareceer the message "User successfully added"

Regards

fox-alvarez commented 11 years ago

good day.

The solution was next, brianc wrote me the following message:


You're using the connection pool with pg.connect

When you are finished with the client do not call end on the client. Return the client to the pool. Currently you're starving out your pool by checking out clients, ending them manually, and never returning them to the pool.

Please see this for more information:

https://github.com/brianc/node-postgres/wiki/pg


the change was as follows hise:

pg.connect(conString, function(err,client) {
        var soc=Math.floor((Math.random()*1000)+1);
        var StringConsulta="select conectados_ins(1,'"+soc+"',true,2)"; 
        var consultaSQL=client.query(StringConsulta);
        consultaSQL.on('row', function(row) {
            client.end.bind(client);
            console.log("User successfully added");
        });
    });

to

pg.connect(conString, function(err,client,done) {
        var soc=Math.floor((Math.random()*1000)+1);
        var StringConsulta="select conectados_ins(1,'"+soc+"',true,2)"; 
        var consultaSQL=client.query(StringConsulta);
        consultaSQL.on('row', function(row) {
            console.log("User successfully added");
            done();
        });
    });

works great regards