What happens to TypeORM abandoned transaction after startTransaction is called

840 Views Asked by At

I am using TypeORM as ORM tool for my node application.

I have a set of statements that I need to run in a single transaction. So I am using queryRunner to manage my transaction.

In my transaction, I get a bunch of entities and based on some logic I either move forward with my transaction or return some error code.

What I want know is After my await queryRunner.startTransaction() call and saving some entities, I encountered an error condition and return with some response, without committing or rollBack.

How does TypeOrm handles it?
Does It just rollback on its own? (Because if i check logs I cant find rollback).

If not, then what happened to that Started Transaction and uncommitted changes?

I know that finally block will be executed and queryRunner will be released for sure.

But what will happen to my uncommitted changes?

const queryRunner = await getConnection().createQueryRunner();
try {
    await queryRunner.connect();
    await queryRunner.startTransaction();
    const t1 = await queryRunner.manager.getRepository(T1).findOneOrFail(opts);
    if(!t1) {
       return Response.build(1001, 'not found');
    }
    .
    . //do some update and inserts
    .
    const t2 = await queryRunner.manager.getRepository(T2).findOneOrFail(opts);
    if(!t2) {
       return Response.build(1001, 'not found');
    }
    .
    . //do some update and inserts
    .
    await queryRunner.commitTransaction();
} catch (e) {
     await queryRunner.rollbackTransaction();
} finally {
     await queryRunner.release();
}
0

There are 0 best solutions below