Skip to content

Commit

Permalink
🐛 Destroy the connection in case of a network failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas2D committed Mar 31, 2024
1 parent 4801888 commit e6689be
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {defaults, map} from "lodash";
import {defaults, map, noop} from "lodash";
import assert from "assert";
import Client from "knex/lib/client";

Expand Down Expand Up @@ -137,10 +137,11 @@ class Client_Firebird extends Client {
throw new Error('this should never happen!')
}

const transaction = await connection.startTransaction();
const statement = await connection.prepare(transaction, sql);
let transaction, statement;

try {
transaction = await connection.startTransaction();
statement = await connection.prepare(transaction, sql);
let fResponse = {
rows: [],
fields: []
Expand All @@ -167,13 +168,26 @@ class Client_Firebird extends Client {


await this._fixResponse(fResponse, transaction)
await transaction.commit()

return {
...obj,
response: fResponse
}
} catch (e) {
if (transaction) {
await transaction.rollback().catch(noop);
transaction = null
}
if (String(e).includes('Error writing data to the connection.')) {
await this.destroyRawConnection(connection)
}
throw e
} finally {
await transaction.commit()
await statement.dispose();
if (statement) {
await statement.dispose().catch(noop);
statement = null
}
}
}

Expand Down

0 comments on commit e6689be

Please sign in to comment.