Search code examples
node.jsamazon-web-servicesamazon-qldb

BadRequestException: no open transaction, qldb, nodejs driver


I set up my nodejs app with qldb to implement a wallet service. Set up some tests with some success tests and some expected error tests and once in a while this error 'BadRequestException: no open transaction' would happen and cause my tests to fail. if I run the test again, they will pass. Again once in a while, this error will happen unexpectedly cause the tests to fail. I noted when commented out my expected error tests and the error didn't happen or did not happen as often. and this error happens not only to the expected error test but to the successful tests. this is how my tests look like

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

Solution

  • Update on this issue. I use nodejs driver version 2.1.0. My team and I found out that the problem was because there are rollbacks that happen after the error tests and we don't know when the rollbacks are done. When the rollback of the previous test is still running, the transaction for that test is still open so if the next test tries to open a new transaction it would conflict and would not able to open a new transaction for the next test. To fix this, we just not throw errors inside the transaction to prevent rollbacks to happen. This way works for our code, but a better solution would be a way to detect when the rollback is done from the driver and wait for the transaction to close before opening a new transaction.