Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-4961)!: remove command result from commit and abort transaction APIs #3784

Merged
merged 5 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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