Skip to content

Commit

Permalink
#642 first version finished
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Aug 15, 2019
1 parent 88ae2d0 commit ef6c3e5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/connect.js
Expand Up @@ -28,7 +28,7 @@ function poolConnect(ctx, db, config) {
reject(err);
return;
}
p.connect((err, client, done) => {
p.connect((err, client) => {
if (err) {
Events.error(ctx.options, err, {
cn: npm.utils.getSafeConnection(ctx.cn),
Expand All @@ -54,9 +54,9 @@ function poolConnect(ctx, db, config) {
resolve({
client,
useCount: client.$useCount,
done(e) {
release() {
client.end = end;
done(e);
client.release(client.$connectionError);
Events.disconnect(ctx, client);
client.removeListener('error', onError);
}
Expand Down Expand Up @@ -86,7 +86,7 @@ function directConnect(ctx, config) {
resolve({
client,
useCount: 0,
done() {
release() {
client.end = end;
client.end();
Events.disconnect(ctx, client);
Expand Down
4 changes: 2 additions & 2 deletions lib/context.js
Expand Up @@ -56,9 +56,9 @@ class ConnectionContext {
this.start = new Date();
}

disconnect(err) {
disconnect() {
if (this.db) {
this.db.done(err);
this.db.release();
this.db = null;
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/query.js
Expand Up @@ -152,6 +152,10 @@ function $query(ctx, query, values, qrm, config) {
ctx.db.client.query(query, params, (err, result) => {
let data, multiResult, lastResult = result;
if (err) {
// istanbul ignore if (auto-testing connectivity issues is too problematic)
if (npm.utils.isConnectivityError(err)) {
ctx.db.client.$connectionError = err;
}
error = err;
} else {
multiResult = Array.isArray(result);
Expand Down
18 changes: 17 additions & 1 deletion lib/utils/index.js
Expand Up @@ -199,6 +199,21 @@ function addInspection(type, cb) {
type.prototype[npm.util.inspect.custom] = cb;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Identifies a general connectivity error, after which no more queries can be executed.
// This is for detecting when to skip executing ROLLBACK for a failed transaction.
function isConnectivityError(err) {
const code = err && typeof err.code === 'string' && err.code;
const cls = code && code.substr(0, 2); // Error Class
// istanbul ignore next (we cannot test-cover all error cases):
return code === 'ECONNRESET' || (cls === '08' && code !== '08P01') || (cls === '57' && code !== '57014');
// Code 'ECONNRESET' - Connectivity issue handled by the driver.
// Class 08 - Connection Exception (except for 08P01, for protocol violation).
// Class 57 - Operator Intervention (except for 57014, for cancelled queries).
//
// ERROR CODES: https://www.postgresql.org/docs/9.6/static/errcodes-appendix.html
}

const exp = {
getIfHas,
addInspection,
Expand All @@ -213,7 +228,8 @@ const exp = {
addReadProperties,
getSafeConnection,
messageGap,
inherits
inherits,
isConnectivityError
};

const mainFile = process.argv[1];
Expand Down

0 comments on commit ef6c3e5

Please sign in to comment.