BadRequestException:没有打开的事务,qldb, nodejs驱动程序



我用qldb设置我的nodejs应用程序来实现钱包服务。设置了一些测试,其中包括一些成功测试和一些预期的错误测试,偶尔会出现错误"BadRequestException: no open transaction",导致我的测试失败。如果我再做一次测试,他们会通过的。偶尔,这个错误会意外发生,导致测试失败。我注意到,当注释掉我预期的错误测试时,错误没有发生,或者不经常发生。这个错误不仅发生在预期的错误测试中,也发生在成功的测试中。这是我的测试的样子

describe('createWallet()', () => {
it('should return an object with wallet Id', async () => {
let result6 = await controller.createWallet({ body: mocks.walletInfo6.info });
documentId6 = result6.walletId;
expect(result6).to.have.property('walletId').that.is.a.uuid;
});
it('One player should have only one active wallet for each currency', async () => {
try {
let res = await controller.createWallet({ body: mocks.walletInfo1.info });
assert.fail('expected error was not thrown')
} catch (e) {
expect(e.message).to.equal('Player already owns an active wallet in this currency.');
}
});
});
describe('suspendWallet()', () => {
it('should change wallet status to suspend', async () => {
let res = await controller.suspendWallet({ documentId: documentId3 });
await controller.suspendWallet({ documentId: documentId5 });
expect(res).to.be.a.string;
expect(res).to.equal(documentId3);
});
it('should not change wallet status if wallet Id is invalid', async () => {
try {
let res = await controller.suspendWallet({ documentId: mocks.invalidWalletId });
assert.fail('expected error was not thrown')
} catch (e) {
expect(e.message).to.equal('Did not find any record with this document Id.');
}
});
});

如果不看看驱动程序是如何执行事务的,很难确定应用程序是如何遇到这个错误的。

驱动api(例如- execute)返回一个promise。应用程序可能会看到"No transaction open"错误。是在发送进一步的命令之前未解析承诺。

Cookbook -参考QLDB JS驱动程序Cookbook,其中列出了CRUD操作的代码示例。请注意示例如何在事务中使用await来等待承诺解析。不等待execute返回的承诺会导致驱动程序在execute调用被处理之前提交事务,因此会出现"No open transaction error"。

执行事务的示例代码-

var qldb = require('amazon-qldb-driver-nodejs');
const qldbDriver = new qldb.QldbDriver("vehicle-registration");
(async function() {
await qldbDriver.executeLambda(async (txn) => {
await txn.execute("CREATE TABLE Person");
});
})();

如果您仍然面临问题,请分享您使用驱动程序执行事务的代码片段。

更新此问题。我使用nodejs驱动程序版本2.1.0。我和我的团队发现这个问题是因为在错误测试之后发生了回滚,我们不知道回滚什么时候完成。当前一个测试的回滚仍在运行时,该测试的事务仍然打开,因此如果下一个测试尝试打开新事务,它将发生冲突,并且无法为下一个测试打开新事务。要解决这个问题,我们只需在事务中不抛出错误,以防止发生回滚。这种方法适用于我们的代码,但更好的解决方案是检测何时从驱动程序完成回滚,并在打开新事务之前等待事务关闭。

最新更新