Skip to content

Change CSOT exception hierarchy#1938

Draft
vbabanin wants to merge 2 commits intomongodb:mainfrom
vbabanin:JAVA-5438
Draft

Change CSOT exception hierarchy#1938
vbabanin wants to merge 2 commits intomongodb:mainfrom
vbabanin:JAVA-5438

Conversation

@vbabanin
Copy link
Copy Markdown
Member

@vbabanin vbabanin commented Apr 11, 2026

MongoTimeoutException is stable public API documented as "waiting for a server or connection to become available," implying the operation was not executed. MongoOperationTimeoutException does not provide that guarantee: the operation may have been sent to the server and may have executed. Keeping it as a subclass of MongoTimeoutException is therefore semantically incorrect and may mislead consumers that catch MongoTimeoutException into assuming no operation was executed.

Changes:

  • Make MongoOperationTimeoutException extend MongoClientException instead of MongoTimeoutException to avoid breaking the semantic guarantee that MongoTimeoutException implies the operation was not executed
  • Handle MongoOperationTimeoutException explicitly in instanceof MongoTimeoutException checks to preserve existing behavior for transaction labeling, connection pool events, and logging

Options considered:

  • extends MongoClientException (what is proposed in this PR) - sibling to MongoTimeoutException. Both are client-side failures, no shared timeout semantics.
  • extends MongoException - too broad. MongoException is the root for everything including server errors. MongoOperationTimeoutException is a client-side concern.
  • New intermediate class like MongoOperationException - adds a class just for one subclass. Unnecessary abstraction.
  • extends RuntimeException directly - breaks out of the MongoException hierarchy entirely. Users catching MongoException would miss it.

JAVA-5438

- Make MongoOperationTimeoutException extend MongoClientException instead of MongoTimeoutException
  to avoid breaking the semantic guarantee that MongoTimeoutException implies the operation was not executed
- Handle MongoOperationTimeoutException explicitly in instanceof MongoTimeoutException checks
  to preserve existing behavior for transaction labeling, connection pool events, and logging

JAVA-5438
@vbabanin vbabanin self-assigned this Apr 11, 2026
@vbabanin vbabanin requested review from Copilot and rozza April 11, 2026 19:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Java driver’s timeout exception semantics by removing the inheritance relationship between MongoOperationTimeoutException and MongoTimeoutException, so that catching MongoTimeoutException continues to imply “timed out waiting for a server/connection” (i.e., the operation was not executed).

Changes:

  • Changed MongoOperationTimeoutException to extend MongoClientException instead of MongoTimeoutException.
  • Updated transaction error labeling and commit labeling logic to treat MongoOperationTimeoutException as a timeout-equivalent case.
  • Updated connection pool/server selection timeout handling and structured logging logic to explicitly account for MongoOperationTimeoutException.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
driver-sync/src/main/com/mongodb/client/internal/MongoClusterImpl.java Adds MongoOperationTimeoutException handling when applying transient transaction error labels.
driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/internal/OperationExecutorImpl.java Adds MongoOperationTimeoutException handling when applying transient transaction error labels in reactive execution.
driver-core/src/main/com/mongodb/MongoOperationTimeoutException.java Changes the exception superclass to MongoClientException to avoid implying “not executed” semantics.
driver-core/src/main/com/mongodb/internal/operation/CommitTransactionOperation.java Treats MongoOperationTimeoutException as eligible for UnknownTransactionCommitResult labeling decisions.
driver-core/src/main/com/mongodb/internal/connection/LoadBalancedCluster.java Broadens timeout-exception factory return type and ensures timeoutMS produces MongoOperationTimeoutException.
driver-core/src/main/com/mongodb/internal/connection/DefaultConnectionPool.java Ensures connection checkout failure classification/maintenance handling treats MongoOperationTimeoutException as a timeout.
driver-core/src/main/com/mongodb/internal/connection/BaseCluster.java Ensures server selection timeout creation/logging properly accounts for MongoOperationTimeoutException.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants