TransactionError:事务已中止。在Transaction._rollback

时间:2019-09-29 18:26:39

标签: javascript node.js sql-server nestjs typeorm

我正在尝试回滚失败的事务,但是它引发了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>)

1 个答案:

答案 0 :(得分:0)

<Detail> <StandardLine> <Stan> <Type>A</Type> <Code>1234</Code> <Value>sdg</Value> </Stan> </StandardLine> <StandardLine> <Stan> <Type>C</Type> <Code>122234</Code> <Value>sdsdgg</Value> <Cate>Thiere</Cate> </Stan> </StandardLine> <StandardLine> <Stan> <Type>1</Type> <Code>7336</Code> <Value>this one</Value> <Stone>diamond</Stone> </Stan> </StandardLine> </Detail> 的作用是什么?可能是控制台记录了Dim xmlDoc, nodes, strQuery, objNode Set xmlDoc = CreateObject("Microsoft.XMLDOM") xmlDoc.Async = "False" 'This is inputing the xml as a string from a previous bit of code xmlDoc.loadXML(XMLOut) strQuery = "//Detail/StandardLine" Set nodes = xmlDoc.documentElement.selectNodes(strQuery) 'Check to see if this xmlDoc has any of the desired SL nodes If nodes.length = 0 Then msgbox "There are no SLine nodes",,"The checker" exit function End If 'Will only get this far if there is a SLine node msgbox "Before Nodes" & vbCrLf & nodes.length,,"Pre loop" ' Will go through each SLinenodes For Each objNode in nodes msgbox "I am in the loop!",,"The box...in the loop" 'Here down is what I want to do but am not able to get it to work 'Need to to process the SLine node Type = objNode.documentElement.selectSingleNode("//Stan/Type").text If Type = 1 Then Code = objNode.documentElement.selectSingleNode("//Stan/Code").text End if 'Will be functions here later to use the variable Code msgbox "Number is: " & Code,,"Thereas the number?" Next msgbox "After Nodes",,"Post loop" ,但我认为这里并没有发现实际的异常。

一种快速的测试方法是删除this.errorService.throwAPIError(err);,看看您是否仍在控制台中收到消息。