This is probably more of a question about pooling then about the module itself.
When I create a pool using
oracledb.createPool (
{
user : "hr"
password : "welcome"
connectString : "localhost/XE"
},
function(err, pool)
{
pool.getConnection (
function(err, connection)
{
. . . // use connection
});
});
How do I use this pool outside of this context? Ie in a different module but within the same app.
For example I have a module that exports something like
module.exports = {
select1: function ()
{
oracledb.createPool (
{
user : "hr"
password : "welcome"
connectString : "localhost/XE"
},
function(err, pool)
{
pool.getConnection (
function(err, connection)
{
connection.execute(" SQL")
});
});
}
select2: function()
{
oracledb.createPool (
{
user : "hr"
password : "welcome"
connectString : "localhost/XE"
},
function(err, pool)
{
pool.getConnection (
function(err, connection)
{
connection.execute(" SQL")
});
});
}
}
right now I do oracledb.createPool for each one of the functions in the module. Is this the correct way to do it? Or should I be doing oracledb.createPool then on success I start the server and then each function(select1, select2) uses pool.getConnection? If so can I just assign my pool connection to a variable and pass it to the functions?
I am just trying to understand the correct way to use connection pooling.
Currently, to create a pool in one module and use/consume it in another, you have to create a wrapper module like this:
var oracledb = require('oracledb');
var poolMap = {};
function createPool(poolName, config, callback) {
oracledb.createPool(
config,
function(err, p) {
if (err) throw err;
poolMap[poolName] = p;
callback();
}
);
}
module.exports.createPool = createPool;
function getPool(poolName) {
return poolMap[poolName];
}
module.exports.getPool = getPool;
You can import that module into a "main" module and use the createPool method (note the poolName param) to create a pool. In another module you can import the wrapper and call the getPool method.
Generally, you'd create just one pool per app, unless of course you needed more for some reason (perhaps multiple DBs and/or users).
Just what I was wanting to do thank you! No idea why it didn't come to me before, guess I was just starring at it for to long.
Most helpful comment
Currently, to create a pool in one module and use/consume it in another, you have to create a wrapper module like this:
You can import that module into a "main" module and use the createPool method (note the poolName param) to create a pool. In another module you can import the wrapper and call the getPool method.
Generally, you'd create just one pool per app, unless of course you needed more for some reason (perhaps multiple DBs and/or users).