now loopback reconects to db
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
3f9446d94f
commit
0715f8ac81
|
@ -246,3 +246,98 @@ exports.initialize = function initialize(dataSource, callback) {
|
|||
dataSource.connector.connect(callback);
|
||||
}
|
||||
};
|
||||
|
||||
MySQL.prototype.connect = function(callback) {
|
||||
const self = this;
|
||||
const options = generateOptions(this.settings);
|
||||
|
||||
if (this.client) {
|
||||
if (callback) {
|
||||
process.nextTick(function() {
|
||||
callback(null, self.client);
|
||||
});
|
||||
}
|
||||
} else
|
||||
this.client = connectionHandler(options, callback);
|
||||
|
||||
function connectionHandler(options, callback) {
|
||||
const client = mysql.createPool(options);
|
||||
client.getConnection(function(err, connection) {
|
||||
const conn = connection;
|
||||
if (!err) {
|
||||
if (self.debug)
|
||||
debug('MySQL connection is established: ' + self.settings || {});
|
||||
|
||||
connection.release();
|
||||
} else {
|
||||
if (err.code == 'ECONNREFUSED' || err.code == 'PROTOCOL_CONNECTION_LOST') { // PROTOCOL_CONNECTION_LOST
|
||||
console.error(`MySQL connection lost (${err.code}). Retrying..`);
|
||||
|
||||
return setTimeout(() =>
|
||||
connectionHandler(options, callback), 5000);
|
||||
}
|
||||
if (self.debug || !callback)
|
||||
console.error('MySQL connection is failed: ' + self.settings || {}, err);
|
||||
}
|
||||
callback && callback(err, conn);
|
||||
});
|
||||
|
||||
return client;
|
||||
}
|
||||
};
|
||||
|
||||
function generateOptions(settings) {
|
||||
const s = settings || {};
|
||||
if (s.collation) {
|
||||
// Charset should be first 'chunk' of collation.
|
||||
s.charset = s.collation.substr(0, s.collation.indexOf('_'));
|
||||
} else {
|
||||
s.collation = 'utf8_general_ci';
|
||||
s.charset = 'utf8';
|
||||
}
|
||||
|
||||
s.supportBigNumbers = (s.supportBigNumbers || false);
|
||||
s.timezone = (s.timezone || 'local');
|
||||
|
||||
if (isNaN(s.connectionLimit))
|
||||
s.connectionLimit = 10;
|
||||
|
||||
let options;
|
||||
if (s.url) {
|
||||
// use url to override other settings if url provided
|
||||
options = s.url;
|
||||
} else {
|
||||
options = {
|
||||
host: s.host || s.hostname || 'localhost',
|
||||
port: s.port || 3306,
|
||||
user: s.username || s.user,
|
||||
password: s.password,
|
||||
timezone: s.timezone,
|
||||
socketPath: s.socketPath,
|
||||
charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd.
|
||||
supportBigNumbers: s.supportBigNumbers,
|
||||
connectionLimit: s.connectionLimit,
|
||||
};
|
||||
|
||||
// Don't configure the DB if the pool can be used for multiple DBs
|
||||
if (!s.createDatabase)
|
||||
options.database = s.database;
|
||||
|
||||
// Take other options for mysql driver
|
||||
// See https://github.com/strongloop/loopback-connector-mysql/issues/46
|
||||
for (const p in s) {
|
||||
if (p === 'database' && s.createDatabase)
|
||||
continue;
|
||||
|
||||
if (options[p] === undefined)
|
||||
options[p] = s[p];
|
||||
}
|
||||
// Legacy UTC Date Processing fallback - SHOULD BE TRANSITIONAL
|
||||
if (s.legacyUtcDateProcessing === undefined)
|
||||
s.legacyUtcDateProcessing = true;
|
||||
|
||||
if (s.legacyUtcDateProcessing)
|
||||
options.timezone = 'Z';
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue