Skip to content

Instantly share code, notes, and snippets.

@devarajchidambaram
Created March 8, 2019 07:45
Show Gist options
  • Save devarajchidambaram/01d214b09172c7de3fbcb6034299ab56 to your computer and use it in GitHub Desktop.
Save devarajchidambaram/01d214b09172c7de3fbcb6034299ab56 to your computer and use it in GitHub Desktop.
generic pool
const genericPool = require("generic-pool");
const DbDriver = require("mysql");
/**
* Step 1 - Create pool using a factory object
*/
const factory = {
create: function() {
return DbDriver.createConnection({
host: '192.168.8.221',
user: 'monty',
password: 'some_pass',
database: 'nodejs'
});
},
destroy: function(client) {
client.disconnect();
}
};
const opts = {
max: 10, // maximum size of the pool
min: 2 // minimum size of the pool
};
const myPool = genericPool.createPool(factory, opts);
const resourcePromise = myPool.acquire();
resourcePromise
.then(function(client) {
client.query("select * from customer", [], function(err, data) {
if(err) throw err;
console.log("data===", data)
// return object back to pool
myPool.release(client);
});
})
.catch(function(err) {
// handle error - this is generally a timeout or maxWaitingClients
// error
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment