Q: Terminate MongoDB transactions when Node.js exists I'm using MongoDB transactions in a Node.js application as well as pessimistic locking explained in this article: How To SELECT ... FOR UPDATE inside MongoDB Transactions
The code looks somewhat like this:
const session = client.startSession();

session.startTransaction(transactionOptions);

try {
  // Locking the document by updating it's field
  await collection.findOneAndUpdate(…, { session });

  await session.commitTransaction();

} catch (error: any) {
  await session.abortTransaction();
  throw error;

} finally {
  await session.endSession();

}

However, I've noticed that when Node.js process accidentally terminates (e.g. killed by the OS), the lock is not released immediately. It takes some time for the lock to be removed.
What could be causing this and what is the best strategy to avoid such hanging transactions?
