In AWS QLDB, I execute the below lambda:
await qldbDriver.executeLambda(async (txn: TransactionExecutor) => {
const check = (await txn.execute('SELECT * FROM Accounts WHERE owner = ?', document.owner)).getResultList();
if (check.length === 0) {
await txn.execute('INSERT INTO Accounts ?', document)
}
});
The above transaction is idempotent. If I test the transaction by running it multiple times (with the same owner
) in parallel, I conclude that the transaction works as expected, and only a single Account
is inserted into QLDB.
QUESTION: Is there any way to know (via callback or rejected promise) if a transaction fails or is successfully committed??
Thanks
Thanks for your interest in QLDB. The lambda function also takes a function via retryConfig parameter which is invoked whenever there is a retryable failure and the driver is about to retry the transaction block. It will be called with the current attempt number. The common way to handle errors are as below since there might be other errors that won't be retried by the driver and bumped up to lambda immediately.
try {
const qldbDriver: QldbDriver = getQldbDriver();
await qldbDriver.executeLambda(async (txn: TransactionExecutor) => {
const check = (await txn.execute('SELECT * FROM Accounts WHERE owner = ?', document.owner)).getResultList();
if (check.length === 0) {
try {
await txn.execute('INSERT INTO Accounts ?', document)
} catch (e) {
// Not recommended. Driver is responsible for retrying recoverable errors like OCC
throw e
}
}
});
} catch (e) {
error(`Unable to insert documents: ${e}`);
}
Please also create an issue here if there is a feature request and help us know your use case better.