croquiscom/cormo

Transaction rollback does not work

luaneyed opened this issue · 2 comments

try {
    await Connection.transaction({ models: [ModelClass] }, (ModelClassTx) => {
      // [Job 1] do something using ModelClassTx
      throw new Error('An intentional error');
      // [Job 2] do something using ModelClassTx

      return Promise.resolve();
    });
  } catch (error) {
    return { error };
  }

I expect the above code affect nothing to db, but Job 1 is committed!
Exactly, the job is committed when MySQLAdapter::rollbackTransaction is executed.

I already have a test case for that.

it('transaction fail', async () => {

Please give a more detail for the problem.

I solved it.
All queries should be executed before returning the transaction block.

I think it's just a matter of order.

[ what I wanted ]

START TRANSACTION
SQL-1
SQL-2
ROLLBACK

[ what happened with my asynchronous code ]

START TRANSACTION
ROLLBACK
SQL-1
SQL-2