Skip to content

Commit 905e94d

Browse files
endTransaction, removed endTransactionAsync
1 parent 689134c commit 905e94d

File tree

3 files changed

+32
-48
lines changed

3 files changed

+32
-48
lines changed

src/sessions.ts

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,14 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
419419
* Commits the currently active transaction in this session.
420420
*/
421421
async commitTransaction(): Promise<void> {
422-
return await endTransactionAsync(this, 'commitTransaction');
422+
return await endTransaction(this, 'commitTransaction');
423423
}
424424

425425
/**
426426
* Aborts the currently active transaction in this session.
427427
*/
428428
async abortTransaction(): Promise<void> {
429-
return await endTransactionAsync(this, 'abortTransaction');
429+
return await endTransaction(this, 'abortTransaction');
430430
}
431431

432432
/**
@@ -637,25 +637,15 @@ async function attemptTransaction<T>(
637637
}
638638
}
639639

640-
const endTransactionAsync = promisify(
641-
endTransaction as (
642-
session: ClientSession,
643-
commandName: 'abortTransaction' | 'commitTransaction',
644-
callback: (error: Error) => void
645-
) => void
646-
);
647-
648-
function endTransaction(
640+
async function endTransaction(
649641
session: ClientSession,
650-
commandName: 'abortTransaction' | 'commitTransaction',
651-
callback: Callback<void>
652-
) {
642+
commandName: 'abortTransaction' | 'commitTransaction'
643+
): Promise<void> {
653644
// handle any initial problematic cases
654645
const txnState = session.transaction.state;
655646

656647
if (txnState === TxnState.NO_TRANSACTION) {
657-
callback(new MongoTransactionError('No transaction started'));
658-
return;
648+
throw new MongoTransactionError('No transaction started');
659649
}
660650

661651
if (commandName === 'commitTransaction') {
@@ -665,37 +655,28 @@ function endTransaction(
665655
) {
666656
// the transaction was never started, we can safely exit here
667657
session.transaction.transition(TxnState.TRANSACTION_COMMITTED_EMPTY);
668-
callback();
669658
return;
670659
}
671660

672661
if (txnState === TxnState.TRANSACTION_ABORTED) {
673-
callback(
674-
new MongoTransactionError('Cannot call commitTransaction after calling abortTransaction')
675-
);
676-
return;
662+
throw new MongoTransactionError('Cannot call commitTransaction after calling abortTransaction');
677663
}
678664
} else {
679665
if (txnState === TxnState.STARTING_TRANSACTION) {
680666
// the transaction was never started, we can safely exit here
681667
session.transaction.transition(TxnState.TRANSACTION_ABORTED);
682-
callback();
683668
return;
684669
}
685670

686671
if (txnState === TxnState.TRANSACTION_ABORTED) {
687-
callback(new MongoTransactionError('Cannot call abortTransaction twice'));
688-
return;
672+
throw new MongoTransactionError('Cannot call abortTransaction twice');
689673
}
690674

691675
if (
692676
txnState === TxnState.TRANSACTION_COMMITTED ||
693677
txnState === TxnState.TRANSACTION_COMMITTED_EMPTY
694678
) {
695-
callback(
696-
new MongoTransactionError('Cannot call abortTransaction after calling commitTransaction')
697-
);
698-
return;
679+
throw new MongoTransactionError('Cannot call abortTransaction after calling commitTransaction');
699680
}
700681
}
701682

@@ -728,9 +709,8 @@ function endTransaction(
728709
if (session.loadBalanced) {
729710
maybeClearPinnedConnection(session, { force: false });
730711
}
731-
732-
// The spec indicates that we should ignore all errors on `abortTransaction`
733-
return callback();
712+
// The spec indicates that if the operation times out or fails with a non-retryable error, we should ignore all errors on `abortTransaction`
713+
return;
734714
}
735715

736716
session.transaction.transition(TxnState.TRANSACTION_COMMITTED);
@@ -751,14 +731,14 @@ function endTransaction(
751731
}
752732
}
753733

754-
callback(error);
734+
throw error;
755735
}
756736

757737
if (session.transaction.recoveryToken) {
758738
command.recoveryToken = session.transaction.recoveryToken;
759739
}
760740

761-
const handleFirstCommandAttempt = (error?: Error) => {
741+
const handleFirstCommandAttempt = async (error?: Error) => {
762742
if (command.abortTransaction) {
763743
// always unpin on abort regardless of command outcome
764744
session.unpin();
@@ -775,29 +755,33 @@ function endTransaction(
775755
});
776756
}
777757

778-
executeOperation(
758+
await executeOperation(
779759
session.client,
780760
new RunAdminCommandOperation(command, {
781761
session,
782762
readPreference: ReadPreference.primary,
783763
bypassPinningCheck: true
784764
})
785-
).then(() => commandHandler(), commandHandler);
786-
return;
765+
).catch(e => commandHandler(e));
766+
commandHandler();
787767
}
788-
789768
commandHandler(error);
790769
};
791770

792-
// send the command
793-
executeOperation(
794-
session.client,
795-
new RunAdminCommandOperation(command, {
796-
session,
797-
readPreference: ReadPreference.primary,
798-
bypassPinningCheck: true
799-
})
800-
).then(() => handleFirstCommandAttempt(), handleFirstCommandAttempt);
771+
try {
772+
// send the command
773+
await executeOperation(
774+
session.client,
775+
new RunAdminCommandOperation(command, {
776+
session,
777+
readPreference: ReadPreference.primary,
778+
bypassPinningCheck: true
779+
})
780+
);
781+
await handleFirstCommandAttempt();
782+
} catch (e) {
783+
await handleFirstCommandAttempt(e);
784+
}
801785
}
802786

803787
/** @public */

test/integration/transactions-convenient-api/transactions-convenient-api.spec.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const SKIPPED_TESTS = [
99
'withTransaction succeeds if callback aborts'
1010
];
1111

12-
describe('Transactions Convenient API Spec Unified Tests', function () {
12+
describe.only('Transactions Convenient API Spec Unified Tests', function () {
1313
beforeEach(function () {
1414
if (this.configuration.topologyType === 'LoadBalanced') {
1515
if (this.currentTest) {

test/integration/transactions/transactions.spec.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const SKIPPED_TESTS = [
1414
'causal consistency disabled'
1515
];
1616

17-
describe('Transactions Spec Unified Tests', function () {
17+
describe.only('Transactions Spec Unified Tests', function () {
1818
runUnifiedSuite(loadSpecTests(path.join('transactions', 'unified')), test => {
1919
return SKIPPED_TESTS.includes(test.description)
2020
? 'TODO(NODE-5924/NODE-5925): Skipping failing transaction tests'

0 commit comments

Comments
 (0)