我正在尝试在节点promise中创建与雪花数据库的连接。
var NodePoolCluster = require('node-pool-cluster');
var snowflake = require('snowflake-sdk');
init() {
this.nodePoolCluster = NodePoolCluster.initCluster({
name: poolName,
destroy: function (connection) {
connection.destroy(function (err, connection) {
if (err) {
log.error('Unable to disconnect from connection with id: ' + connection.getId() +
'. Message is: ' + err.message);
} else {
log.info('Disconnected connection with id: ' + connection.getId());
}
});
},
max: maxConnection,
min: minConnection,
idleTimeoutMillis: idleTimeoutMillis
});
var poolCluster = this.nodePoolCluster;
return new Promise(function (fulfill, reject) {
console.log("creating pool");
poolCluster.addPool(function (callback) {
console.log("adding pool");
var connection = snowflake.createConnection({
account: snowflakeAccountID,
username: snowflakeUsername,
password: snowflakePassword,
database: snowflakeDatabase,
schema: snowflakeSchema,
warehouse: snowflakeWarehouse,
role: snowflakeRole
});
console.log("connecting to snowflake");
connection.connect(function (err, conn) {
console.log("Inside create connection");
if (err) {
log.error('Unable to connect: ' + err.message);
reject(new Error(err));
return;
} else {
log.info('Successfully connected as id: ' + connection.getId());
fulfill(callback(err, connection));
}
});
});
});
}
这是承诺电话。
var snowFlakePromise = snowflakeObject.init();
snowFlakePromise.then(function () {
console.log(connection created);
}, function (onReject) {
console.log("connection rejected" + onReject.message);
});
这里我在“创建池”后没有得到任何东西,它无法将池添加到集群中。 它的工作正常,没有承诺,但承诺它会产生问题。
没有承诺init()函数就像
init() {
this.nodePoolCluster = NodePoolCluster.initCluster({
name: poolName,
destroy: function (connection) {
connection.destroy(function (err, connection) {
if (err) {
log.error('Unable to disconnect from connection with id: ' + connection.getId() +
'. Message is: ' + err.message);
} else {
log.info('Disconnected connection with id: ' + connection.getId());
}
});
},
max: maxConnection,
min: minConnection,
idleTimeoutMillis: idleTimeoutMillis
});
this.nodePoolCluster.addPool(function (callback) {
var connection = snowflake.createConnection({
account: snowflakeAccountID,
username: snowflakeUsername,
password: snowflakePassword,
database: snowflakeDatabase,
schema: snowflakeSchema,
warehouse: snowflakeWarehouse,
role: snowflakeRole
});
connection.connect(function (err, conn) {
if (err) {
log.error('Unable to connect: ' + err.message);
} else {
log.info('Successfully connected as id: ' + connection.getId());
callback(err, connection);
}
});
});
}