Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example pool with node-oracle that calls pool.destroy() on connection errors.
var genericPool = require('generic-pool');
// This is the 'nearinfinity' driver: `npm install oracle`
var oracle = require('oracle');
conf = {
"hostName": "localhost",
"port": 1521,
"user": "oracle",
"password": "oracle",
"database": "xe",
}
var query = "SELECT systimestamp FROM dual";
var pool = genericPool.Pool({
name: 'testpool',
log: true,
max: 20,
create: function(callback) {
var settings = {
hostname: conf.hostName,
port: conf.port,
database: conf.database,
user: conf.user,
password: conf.password
}
new oracle.connect(settings, function(err, connection) {
callback(err, connection);
});
},
destroy: function(connection) {
connection.close();
}
});
var acquireAndQuery = function() {
pool.acquire(function(err, connection) {
if (err) {
console.log(err, "Error acquiring from pool.");
return;
}
connection.execute(query, [], function(err, results) {
if (err) {
console.log(err, "Error executing query.");
// Simply releasing this connection back to the pool means a potentially
// corrupt connection may get reused.
pool.release(connection)
// This solves the issue
// pool.destroy(connection);
return;
}
pool.release(connection);
console.log(results);
});
});
};
// Continuously make DB calls
setInterval(acquireAndQuery, (3000));
setInterval(acquireAndQuery, (3100));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment