1

我用 qldb 设置了我的 nodejs 应用程序来实现钱包服务。使用一些成功测试和一些预期的错误测试设置一些测试,偶尔会发生此错误“BadRequestException:没有打开的事务”并导致我的测试失败。如果我再次运行测试,它们将通过。偶尔,这个错误会意外发生,导致测试失败。当注释掉我预期的错误测试并且错误没有发生或没有经常发生时,我注意到了。这个错误不仅发生在预期的错误测试中,而且发生在成功的测试中。这就是我的测试的样子

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.');
            }
        });
    });
4

2 回答 2

2

如果不查看驱动程序是如何用于执行事务的,就很难确定您的应用程序是如何遇到此错误的。

驱动程序 API(例如 - execute)返回一个承诺。应用程序可能看到“无事务打开错误”的一种方式是在发送进一步命令之前未解决承诺。

Cookbook - 请参阅 QLDB JS 驱动程序说明书其中列出了 CRUD 操作的代码示例。请注意示例如何在事务中使用 await 来等待 Promise 解决。不等待执行返回的承诺会导致驱动程序在执行调用被处理之前提交事务,因此会出现“没有打开事务错误”。

执行交易的示例代码 -

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");
    });
})();

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

于 2021-02-25T22:06:41.417 回答
1

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

于 2021-02-22T19:03:18.530 回答