Я пытаюсь откатить неудачную транзакцию, но она выдает TransactionError
Я попытался захватить исключение, почему произошел сбой, но отображается только ошибка.
Я пытаюсь вставитьстрока в поле int, которая вызывает исключение.
async create(tenantid: string, staff: StaffDto): Promise < any > {
const connection = getConnection();
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const userQuery = `insert into Users(Username, Password) Output Inserted.Id values('${User.Username}', '${User.Password)
}')`;
await queryRunner.query(userQuery);
const query = `insert into Address(CountryId,StateProvinceId,City,Address1,Address2,ZipPostalCode,PhoneNumber ) Output Inserted.Id values('${staff.Address.CountryId}','${staff.Address.StateProvinceId}','${staff.Address.City}','${staff.Address.Address1}','${staff.Address.Address2}','${staff.Address.ZipPostalCode}','${staff.Address.PhoneNumber}')`;
await queryRunner.query(query);
await queryRunner.commitTransaction();
return {
...staff,
Id: userQueryResult[0].Id,
};
} catch (err) {
await queryRunner.rollbackTransaction();
this.errorService.throwAPIError(err);
} finally {
await queryRunner.release();
}
}
TransactionError: Transaction has been aborted.
at Transaction._rollback (/Users/xxx/Documents/codes/yyy/node_modules/mssql/lib/base.js:984:37)
at Transaction._rollback (/Users/xxx/Documents/codes/yyy/node_modules/mssql/lib/tedious.js:370:11)
at Transaction.rollback (/Users/xxx/Documents/codes/yyy/node_modules/mssql/lib/base.js:957:12)
at /Users/xxx/Documents/codes/yyy/src/driver/sqlserver/SqlServerQueryRunner.ts:156:37
at new Promise (<anonymous>)
at SqlServerQueryRunner.<anonymous> (/Users/xxx/Documents/codes/yyy/src/driver/sqlserver/SqlServerQueryRunner.ts:155:16)
at step (/Users/xxx/Documents/codes/yyy/node_modules/tslib/tslib.js:136:27)
at Object.next (/Users/xxx/Documents/codes/yyy/node_modules/tslib/tslib.js:117:57)
at /Users/xxx/Documents/codes/yyy/node_modules/tslib/tslib.js:110:75
at new Promise (<anonymous>)