Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions source/sessions/driver-sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,11 @@ up with an invalid cluster time. Worse, this invalid cluster time may remain the
clocks between different clusters are not synced. This results in all operations failing until the application is
restarted. To fix this issue we decided it was more robust to stop gossiping on SDAM commands altogether.

## Idiomatic APIs

Drivers MAY implement session management APIs idiomatic to their language. These APIs SHOULD take advantage of language
runtime capabilities to encourage correct usage of sessions.

## Test Plan

See the [README](tests/README.md) for tests.
Expand Down Expand Up @@ -884,6 +889,9 @@ collection.insertOne(session, {})
session.endSession()
```

If possible, drivers are encouraged to implement APIs idiomatic to their language that remove the need to explicitly
pass sessions to each operation.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we mention some conflict resolution strategies here? In case the language idiomatic way lets user to pass the explicit session into the methods, we probably should either check that sessions are the same, or throw, or ignore one of them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idiomatic API still needs to conform to the spec as a whole. It would be difficult to compile a list of possible conflicts and their resolutions for all potential languages.

Do you have a specific scenario in mind?

Copy link
Copy Markdown
Member

@sanych-sun sanych-sun Mar 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree there is no way to have it strictly defined requirements here, but for example in dotnet ecosystem we have something similar called TransactionScope. If we were to adopt it, the user's code could look like this:

using var transaction = new TransactionScope()
{
  await mongoClient.InsertMany([document1, document2]);
  await mongoClient.Delete([document3]);
}

Such code should imply usage of the same transaction for both commands (we do not support this right now, but in theory).

What I'm trying to say, such approach let user to write the following code:

var session = mongoClient.StartSession();
using var transaction = new TransactionScope()
{
  await mongoClient.InsertMany(session, [document1, document2]);
  await mongoClient.Delete([document3]);
}

In such case it's unclear which session to use for InsertMany: throw or ignore one of them?

Should we specify something like:

In case idiomatic and explicit way to specify a session are in use and resolved to different sessions, Driver should prefer the explicitly provided session (or throw?).


### Why does a network error cause the `ServerSession` to be discarded from the pool?

When a network error is encountered when executing an operation with a `ClientSession`, the operation may be left
Expand Down Expand Up @@ -934,6 +942,7 @@ has risks that do not justify the potential guaranteed `ServerSession` allocatio

## Changelog

- 2026-03-12: Drivers MAY implement idiomatic session management APIs.
- 2025-02-24: Drivers MUST NOT gossip $clusterTime on SDAM commands.
- 2024-05-08: Migrated from reStructuredText to Markdown.
- 2017-09-13: If causalConsistency option is omitted assume true
Expand Down
Loading