Skip to content

Commit

Permalink
feat(NODE-4961)!: remove command result from commit and abort transac…
Browse files Browse the repository at this point in the history
…tion APIs (#3784)
  • Loading branch information
nbbeeken committed Jul 31, 2023
1 parent ada1f75 commit 71c5936
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,14 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
/**
* Commits the currently active transaction in this session.
*/
async commitTransaction(): Promise<Document> {
async commitTransaction(): Promise<void> {
return endTransactionAsync(this, 'commitTransaction');
}

/**
* Aborts the currently active transaction in this session.
*/
async abortTransaction(): Promise<Document> {
async abortTransaction(): Promise<void> {
return endTransactionAsync(this, 'abortTransaction');
}

Expand Down Expand Up @@ -636,14 +636,14 @@ const endTransactionAsync = promisify(
endTransaction as (
session: ClientSession,
commandName: 'abortTransaction' | 'commitTransaction',
callback: (error: Error, result: Document) => void
callback: (error: Error) => void
) => void
);

function endTransaction(
session: ClientSession,
commandName: 'abortTransaction' | 'commitTransaction',
callback: Callback<Document>
callback: Callback<void>
) {
// handle any initial problematic cases
const txnState = session.transaction.state;
Expand Down Expand Up @@ -717,7 +717,7 @@ function endTransaction(
Object.assign(command, { maxTimeMS: session.transaction.options.maxTimeMS });
}

function commandHandler(error?: Error, result?: Document) {
function commandHandler(error?: Error) {
if (commandName !== 'commitTransaction') {
session.transaction.transition(TxnState.TRANSACTION_ABORTED);
if (session.loadBalanced) {
Expand Down Expand Up @@ -746,7 +746,7 @@ function endTransaction(
}
}

callback(error, result);
callback(error);
}

if (session.transaction.recoveryToken) {
Expand All @@ -761,7 +761,7 @@ function endTransaction(
readPreference: ReadPreference.primary,
bypassPinningCheck: true
}),
(error, result) => {
error => {
if (command.abortTransaction) {
// always unpin on abort regardless of command outcome
session.unpin();
Expand Down Expand Up @@ -789,7 +789,7 @@ function endTransaction(
);
}

commandHandler(error, result);
commandHandler(error);
}
);
}
Expand Down
33 changes: 33 additions & 0 deletions test/integration/transactions/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,39 @@ describe('Transactions', function () {
});
});

context('when completing a transaction', () => {
let client: MongoClient;
beforeEach(async function () {
client = this.configuration.newClient();
});

afterEach(async function () {
await client.close();
});

it(
'commitTransaction() resolves void',
{ requires: { mongodb: '>=4.2.0', topology: '!single' } },
async () =>
client.withSession(async session =>
session.withTransaction(async session => {
expect(await session.commitTransaction()).to.be.undefined;
})
)
);

it(
'abortTransaction() resolves void',
{ requires: { mongodb: '>=4.2.0', topology: '!single' } },
async () =>
client.withSession(async session =>
session.withTransaction(async session => {
expect(await session.abortTransaction()).to.be.undefined;
})
)
);
});

describe('TransientTransactionError', function () {
it('should have a TransientTransactionError label inside of a transaction', {
metadata: { requires: { topology: 'replicaset', mongodb: '>=4.0.0' } },
Expand Down

0 comments on commit 71c5936

Please sign in to comment.