UNPKG

@breautek/storm

Version:

Object-Oriented REST API framework

93 lines (90 loc) 3.99 kB
"use strict"; /* Copyright 2017-2023 Norman Breau Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Transaction = void 0; const IsolationLevel_1 = require("./IsolationLevel"); const InternalError_1 = require("./InternalError"); const DeadLockError_1 = require("./DeadLockError"); const InvalidValueError_1 = require("./InvalidValueError"); const LockWaitTimeoutError_1 = require("./LockWaitTimeoutError"); const TAG = 'Transaction'; /** * A class encapsulating an entire transaction from beginning to commitment. * * This encapsulates a routine to conduct for the transaction. * Should the transaction fail due to a deadlock, the transaction will automatically * be tried. * * NOTE: It is unsafe to run two transactions on the same connection concurrently. * The execution *must* be waited to complete before executing another transaction, * on the same connection. */ class Transaction { constructor(app, executor, retryLimit = Infinity, isolationLevel = IsolationLevel_1.IsolationLevel.REPEATABLE_READ) { this.$application = app; this.$executor = executor; if (retryLimit === null || retryLimit === undefined) { retryLimit = Infinity; } else if (retryLimit <= 0) { throw new InvalidValueError_1.InvalidValueError('retryLimit', 'integer > 0', retryLimit); } this.$retryLimit = retryLimit; this.$isolationLevel = isolationLevel; } async onPreQuery(connection) { } async onPostQuery(connection) { } getQuery(connection) { return null; } getParametersForQuery() { return null; } async onPostProcess(connection, results) { return null; } async execute(connection) { if (connection.isTransaction()) { throw new InternalError_1.InternalError('Connection must not be in an active transaction. Commit your current transaction first.'); } let attemptCount = 0; do { attemptCount++; this.$application.getLogger().info(TAG, `Starting transaction attempt ${attemptCount} of ${this.$retryLimit === Infinity ? 'Infinity' : this.$retryLimit.toString()}`); await connection.startTransaction(this.$isolationLevel); try { await this.$executor(connection); await connection.commit(); // If we made it here, we can break out of our retry loop break; } catch (ex) { if (attemptCount < this.$retryLimit && ex instanceof DeadLockError_1.DeadLockError) { this.$application.getLogger().warn(TAG, `Deadlock received... retrying transaction`); } else if (attemptCount < this.$retryLimit && ex instanceof LockWaitTimeoutError_1.LockWaitTimeoutError) { this.$application.getLogger().warn(TAG, `Lock timeout... retrying transaction`); // The server does not auto rollback a timeout (it may be configured to do so, but we will assume that it's not) await connection.rollback(); } else { await connection.rollback(); throw ex; } } } while (attemptCount < this.$retryLimit); } } exports.Transaction = Transaction; //# sourceMappingURL=Transaction.js.map