Last active
March 5, 2016 04:34
-
-
Save johannish/6242411 to your computer and use it in GitHub Desktop.
Example pool with node-oracle that calls pool.destroy() on connection errors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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