#Application Freeze and Performance Concerns when using transactions

1 messages · Page 1 of 1 (latest)

high atlas
#

Hi,

We are currently utilizing NestJS in combination with TypeORM and Postgres. However, we're encountering an issue wherein our application freezes when multiple calls are made to the controller using the service method below. Am I missing something on the way how transaction works ?

async processBatches(createUpdateBatchesDTO: any, response: any) {
    const startTime = performance.now();
    const queryRunner = this.connection.createQueryRunner();
    await queryRunner.connect();
    try {
        await queryRunner.startTransaction();
        for (const batch of createUpdateBatchesDTO?.data) {
            await this.handleBatch(batch, queryRunner);
        }
        await queryRunner.commitTransaction();

        response.status(HttpStatus.OK).send({ message: `Batch processing completed for ${createUpdateBatchesDTO?.data?.length} batch(es).` });
    } catch (error) {
        response.status(HttpStatus.INTERNAL_SERVER_ERROR).send({ message: `Error during batch processing`, error });
        await queryRunner.rollbackTransaction();
    } finally {
        const releaseStartTime = performance.now();
        await queryRunner.release();
        const releaseFinishTime = performance.now();

        this.logger.debug(
            `Total processing time for releasing ${createUpdateBatchesDTO.data?.length} batches: ${releaseFinishTime - releaseStartTime} ms.`
        );

        const finishTime = performance.now();
        this.logger.debug(
            `Total processing time for queries of ${createUpdateBatchesDTO.data?.length} batches: ${finishTime - startTime} ms.`
        );
    }
}

high atlas
#

before the app freeze,we're getting deadlock db errors :

query failed: UPDATE "task" SET "batchId" = $2 WHERE "id" = $1 -- PARAMETERS: [6067476,null]
error: error: deadlock detected
    at Connection.parseE (/USR/myApp/organization/myApp-api/node_modules/pg/lib/connection.js:600:48)
    at Connection.parseMessage (/USR/myApp/organization/myApp-api/node_modules/pg/lib/connection.js:399:19)
    at Socket.<anonymous> (/USR/myApp/organization/myApp-api/node_modules/pg/lib/connection.js:115:22)
    at Socket.emit (events.js:311:20)
    at addChunk (_stream_readable.js:294:12)
    at readableAddChunk (_stream_readable.js:275:11)
    at Socket.Readable.push (_stream_readable.js:209:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
  name: 'error',
  length: 408,
  severity: 'ERROR',
  code: '40P01',
  detail: 'Process 43017 waits for ExclusiveLock on tuple (37233,49) of relation 57140 of database 17383; blocked by process 43661.\n' +
    'Process 43661 waits for ShareLock on transaction 437860261; blocked by process 43439.\n' +
    'Process 43439 waits for ShareLock on transaction 437860268; blocked by process 43017.',
  hint: 'See server log for query details.',
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'deadlock.c',
  line: '1146',
  routine: 'DeadLockReport'
}