简体   繁体   中英

Closing postgres (pg) client connection in node.js

I have a script that I want to run on a scheduled basis in node. The script is not terminating and exiting. I suspect that this is because my database client is still open.

var client = new pg.Client(conString);
client.connect();

function registerBundle (innerHash, outterHash) {
    // some stuff here
}

var query = client.query("SELECT id, chain FROM mytable where \
    state_ready = true and transaction_id='' ");

query.on('row', function(row) {
    var chain = row['chain'];
    var pg_record = row['id'];
    console.log(pg_record);
    var innerHash = "something";
    var outerHash = "something else";
    var registrar = registerBundle(innerHash, outerHash);

    var update = client.query('UPDATE mytable SET transaction_id = $1::text \
    where id=$2::int', [transactionHash, pg_record]);
    console.log(chain);
});

if I include the following, the client connection closes before the updates have a time to fire.

query.on('end', function() {
    client.end();
});

I cannot use setTimeout or any other such mechanism because I don't know how long to wait for the registerBundle function to complete. Also I think query.on('end' will fire when the update is completed. Not sure how to test this.

My question, I need things to fire in order.

  • Query DB
  • Process each row (query.on
  • Update each row with value returned from registerBundle
  • Close db client/connection when all rows have been processed.
  • Terminate script and exit node

Seems pretty straightforward from a python/php world but falls apart in my javascript world.

A promise-based interface like pg-promise is the way to go:

var bluebird = require('bluebird');
var pgp = require('pg-promise')({
    promiseLib: bluebird
});
var db = pgp(/*connection details*/);

db.tx(t => {
    // BEGIN executed
    return t.map('SELECT id, chain FROM mytable where state_ready = $1 and transaction_id = $2', [true, 123], a => {
        var chain = data.chain;
        var pg_record = data.id;
        return t.none('UPDATE mytable SET transaction_id = $1::text where id=$2::int', [transactionHash, pg_record]);
    }).then(t.batch); // settling all internal queries
})
    .then(data => {
        // success, COMMIT executed
    })
    .catch(error => {
        // error, ROLLBACK executed
    })
    .finally(pgp.end); // shuts down the connection pool

The example above does exactly what you asked for, plus it uses a transaction. But in reality you're gonna want to do it all in one query, for performance reasons ;)

See more examples .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM