From 8692c7daec56e05030404238a51864c928e0af16 Mon Sep 17 00:00:00 2001 From: Matt Dale <9760375+matthewdale@users.noreply.github.com> Date: Wed, 8 Jul 2026 21:47:21 -0700 Subject: [PATCH 1/4] GODRIVER-3889 Move commitTransaction and abortTransaction to the mongo package. --- mongo/op_abort_transaction.go | 80 ++++++ mongo/op_commit_transaction.go | 80 ++++++ mongo/session.go | 50 ++-- x/mongo/driver/operation/abort_transaction.go | 249 ------------------ .../driver/operation/commit_transaction.go | 238 ----------------- 5 files changed, 195 insertions(+), 502 deletions(-) create mode 100644 mongo/op_abort_transaction.go create mode 100644 mongo/op_commit_transaction.go delete mode 100644 x/mongo/driver/operation/abort_transaction.go delete mode 100644 x/mongo/driver/operation/commit_transaction.go diff --git a/mongo/op_abort_transaction.go b/mongo/op_abort_transaction.go new file mode 100644 index 000000000..6c0a2cccb --- /dev/null +++ b/mongo/op_abort_transaction.go @@ -0,0 +1,80 @@ +// Copyright (C) MongoDB, Inc. 2019-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package mongo + +import ( + "context" + "errors" + + "go.mongodb.org/mongo-driver/v2/event" + "go.mongodb.org/mongo-driver/v2/internal/driverutil" + "go.mongodb.org/mongo-driver/v2/internal/logger" + "go.mongodb.org/mongo-driver/v2/mongo/writeconcern" + "go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore" + "go.mongodb.org/mongo-driver/v2/x/mongo/driver" + "go.mongodb.org/mongo-driver/v2/x/mongo/driver/description" + "go.mongodb.org/mongo-driver/v2/x/mongo/driver/session" +) + +// abortTransactionOp performs an abortTransaction operation. +type abortTransactionOp struct { + authenticator driver.Authenticator + recoveryToken bsoncore.Document + session *session.Client + clock *session.ClusterClock + monitor *event.CommandMonitor + crypt driver.Crypt + database string + deployment driver.Deployment + selector description.ServerSelector + writeConcern *writeconcern.WriteConcern + retry *driver.RetryMode + maxAdaptiveRetries uint + enableOverloadRetargeting bool + serverAPI *driver.ServerAPIOptions + logger *logger.Logger +} + +func (at *abortTransactionOp) processResponse(context.Context, bsoncore.Document, driver.ResponseInfo) error { + return nil +} + +// Execute runs this operations and returns an error if the operation did not execute successfully. +func (at *abortTransactionOp) Execute(ctx context.Context) error { + if at.deployment == nil { + return errors.New("the abortTransaction operation must have a Deployment set before Execute can be called") + } + + return driver.Operation{ + CommandFn: at.command, + ProcessResponseFn: at.processResponse, + RetryMode: at.retry, + Type: driver.Write, + Client: at.session, + Clock: at.clock, + CommandMonitor: at.monitor, + MaxAdaptiveRetries: at.maxAdaptiveRetries, + EnableOverloadRetargeting: at.enableOverloadRetargeting, + Crypt: at.crypt, + Database: at.database, + Deployment: at.deployment, + Selector: at.selector, + WriteConcern: at.writeConcern, + ServerAPI: at.serverAPI, + Name: driverutil.AbortTransactionOp, + Authenticator: at.authenticator, + Logger: at.logger, + }.Execute(ctx) +} + +func (at *abortTransactionOp) command(dst []byte, _ description.SelectedServer) ([]byte, error) { + dst = bsoncore.AppendInt32Element(dst, "abortTransaction", 1) + if at.recoveryToken != nil { + dst = bsoncore.AppendDocumentElement(dst, "recoveryToken", at.recoveryToken) + } + return dst, nil +} diff --git a/mongo/op_commit_transaction.go b/mongo/op_commit_transaction.go new file mode 100644 index 000000000..92326407c --- /dev/null +++ b/mongo/op_commit_transaction.go @@ -0,0 +1,80 @@ +// Copyright (C) MongoDB, Inc. 2019-present. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + +package mongo + +import ( + "context" + "errors" + + "go.mongodb.org/mongo-driver/v2/event" + "go.mongodb.org/mongo-driver/v2/internal/driverutil" + "go.mongodb.org/mongo-driver/v2/internal/logger" + "go.mongodb.org/mongo-driver/v2/mongo/writeconcern" + "go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore" + "go.mongodb.org/mongo-driver/v2/x/mongo/driver" + "go.mongodb.org/mongo-driver/v2/x/mongo/driver/description" + "go.mongodb.org/mongo-driver/v2/x/mongo/driver/session" +) + +// commitTransactionOp attempts to commit a transaction. +type commitTransactionOp struct { + authenticator driver.Authenticator + recoveryToken bsoncore.Document + session *session.Client + clock *session.ClusterClock + monitor *event.CommandMonitor + crypt driver.Crypt + database string + deployment driver.Deployment + selector description.ServerSelector + writeConcern *writeconcern.WriteConcern + retry *driver.RetryMode + maxAdaptiveRetries uint + enableOverloadRetargeting bool + serverAPI *driver.ServerAPIOptions + logger *logger.Logger +} + +func (ct *commitTransactionOp) processResponse(context.Context, bsoncore.Document, driver.ResponseInfo) error { + return nil +} + +// Execute runs this operations and returns an error if the operation did not execute successfully. +func (ct *commitTransactionOp) Execute(ctx context.Context) error { + if ct.deployment == nil { + return errors.New("the commitTransaction operation must have a Deployment set before Execute can be called") + } + + return driver.Operation{ + CommandFn: ct.command, + ProcessResponseFn: ct.processResponse, + RetryMode: ct.retry, + Type: driver.Write, + Client: ct.session, + Clock: ct.clock, + CommandMonitor: ct.monitor, + MaxAdaptiveRetries: ct.maxAdaptiveRetries, + EnableOverloadRetargeting: ct.enableOverloadRetargeting, + Crypt: ct.crypt, + Database: ct.database, + Deployment: ct.deployment, + Selector: ct.selector, + WriteConcern: ct.writeConcern, + ServerAPI: ct.serverAPI, + Name: driverutil.CommitTransactionOp, + Authenticator: ct.authenticator, + Logger: ct.logger, + }.Execute(ctx) +} + +func (ct *commitTransactionOp) command(dst []byte, _ description.SelectedServer) ([]byte, error) { + dst = bsoncore.AppendInt32Element(dst, "commitTransaction", 1) + if ct.recoveryToken != nil { + dst = bsoncore.AppendDocumentElement(dst, "recoveryToken", ct.recoveryToken) + } + return dst, nil +} diff --git a/mongo/session.go b/mongo/session.go index 905ca936c..e98a42460 100644 --- a/mongo/session.go +++ b/mongo/session.go @@ -19,7 +19,6 @@ import ( "go.mongodb.org/mongo-driver/v2/mongo/options" "go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore" "go.mongodb.org/mongo-driver/v2/x/mongo/driver" - "go.mongodb.org/mongo-driver/v2/x/mongo/driver/operation" "go.mongodb.org/mongo-driver/v2/x/mongo/driver/session" ) @@ -273,13 +272,24 @@ func (s *Session) AbortTransaction(ctx context.Context) error { selector := makePinnedSelector(s.clientSession, &serverselector.Write{}) s.clientSession.Aborting = true - _ = operation.NewAbortTransaction().Session(s.clientSession).ClusterClock(s.client.clock).Database("admin"). - Deployment(s.deployment).WriteConcern(s.clientSession.CurrentWc).ServerSelector(selector). - Retry(driver.RetryOncePerCommand).MaxAdaptiveRetries(s.client.effectiveAdaptiveRetries(true)). - EnableOverloadRetargeting(s.client.enableOverloadRetargeting). - CommandMonitor(s.client.monitor). - RecoveryToken(bsoncore.Document(s.clientSession.RecoveryToken)).ServerAPI(s.client.serverAPI). - Authenticator(s.client.authenticator).Logger(s.client.logger).Execute(ctx) + retry := driver.RetryOncePerCommand + op := abortTransactionOp{ + session: s.clientSession, + clock: s.client.clock, + database: "admin", + deployment: s.deployment, + writeConcern: s.clientSession.CurrentWc, + selector: selector, + retry: &retry, + maxAdaptiveRetries: s.client.effectiveAdaptiveRetries(true), + enableOverloadRetargeting: s.client.enableOverloadRetargeting, + monitor: s.client.monitor, + recoveryToken: bsoncore.Document(s.clientSession.RecoveryToken), + serverAPI: s.client.serverAPI, + authenticator: s.client.authenticator, + logger: s.client.logger, + } + _ = op.Execute(ctx) s.clientSession.Aborting = false _ = s.clientSession.AbortTransaction() @@ -309,13 +319,23 @@ func (s *Session) CommitTransaction(ctx context.Context) error { selector := makePinnedSelector(s.clientSession, &serverselector.Write{}) s.clientSession.Committing = true - op := operation.NewCommitTransaction(). - Session(s.clientSession).ClusterClock(s.client.clock).Database("admin").Deployment(s.deployment). - WriteConcern(s.clientSession.CurrentWc).ServerSelector(selector).Retry(driver.RetryOncePerCommand). - MaxAdaptiveRetries(s.client.effectiveAdaptiveRetries(true)). - EnableOverloadRetargeting(s.client.enableOverloadRetargeting). - CommandMonitor(s.client.monitor).RecoveryToken(bsoncore.Document(s.clientSession.RecoveryToken)). - ServerAPI(s.client.serverAPI).Authenticator(s.client.authenticator).Logger(s.client.logger) + retry := driver.RetryOncePerCommand + op := commitTransactionOp{ + session: s.clientSession, + clock: s.client.clock, + database: "admin", + deployment: s.deployment, + writeConcern: s.clientSession.CurrentWc, + selector: selector, + retry: &retry, + maxAdaptiveRetries: s.client.effectiveAdaptiveRetries(true), + enableOverloadRetargeting: s.client.enableOverloadRetargeting, + monitor: s.client.monitor, + recoveryToken: bsoncore.Document(s.clientSession.RecoveryToken), + serverAPI: s.client.serverAPI, + authenticator: s.client.authenticator, + logger: s.client.logger, + } err = op.Execute(ctx) // Return error without updating transaction state if it is a timeout, as the transaction has not diff --git a/x/mongo/driver/operation/abort_transaction.go b/x/mongo/driver/operation/abort_transaction.go deleted file mode 100644 index 2b15056f1..000000000 --- a/x/mongo/driver/operation/abort_transaction.go +++ /dev/null @@ -1,249 +0,0 @@ -// Copyright (C) MongoDB, Inc. 2019-present. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. You may obtain -// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - -package operation - -import ( - "context" - "errors" - - "go.mongodb.org/mongo-driver/v2/event" - "go.mongodb.org/mongo-driver/v2/internal/driverutil" - "go.mongodb.org/mongo-driver/v2/internal/logger" - "go.mongodb.org/mongo-driver/v2/mongo/writeconcern" - "go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore" - "go.mongodb.org/mongo-driver/v2/x/mongo/driver" - "go.mongodb.org/mongo-driver/v2/x/mongo/driver/description" - "go.mongodb.org/mongo-driver/v2/x/mongo/driver/session" -) - -// AbortTransaction performs an abortTransaction operation. -type AbortTransaction struct { - authenticator driver.Authenticator - recoveryToken bsoncore.Document - session *session.Client - clock *session.ClusterClock - collection string - monitor *event.CommandMonitor - crypt driver.Crypt - database string - deployment driver.Deployment - selector description.ServerSelector - writeConcern *writeconcern.WriteConcern - retry *driver.RetryMode - maxAdaptiveRetries uint - enableOverloadRetargeting bool - serverAPI *driver.ServerAPIOptions - logger *logger.Logger -} - -// NewAbortTransaction constructs and returns a new AbortTransaction. -func NewAbortTransaction() *AbortTransaction { - return &AbortTransaction{} -} - -func (at *AbortTransaction) processResponse(context.Context, bsoncore.Document, driver.ResponseInfo) error { - return nil -} - -// Execute runs this operations and returns an error if the operation did not execute successfully. -func (at *AbortTransaction) Execute(ctx context.Context) error { - if at.deployment == nil { - return errors.New("the AbortTransaction operation must have a Deployment set before Execute can be called") - } - - return driver.Operation{ - CommandFn: at.command, - ProcessResponseFn: at.processResponse, - RetryMode: at.retry, - Type: driver.Write, - Client: at.session, - Clock: at.clock, - CommandMonitor: at.monitor, - MaxAdaptiveRetries: at.maxAdaptiveRetries, - EnableOverloadRetargeting: at.enableOverloadRetargeting, - Crypt: at.crypt, - Database: at.database, - Deployment: at.deployment, - Selector: at.selector, - WriteConcern: at.writeConcern, - ServerAPI: at.serverAPI, - Name: driverutil.AbortTransactionOp, - Authenticator: at.authenticator, - Logger: at.logger, - }.Execute(ctx) -} - -func (at *AbortTransaction) command(dst []byte, _ description.SelectedServer) ([]byte, error) { - dst = bsoncore.AppendInt32Element(dst, "abortTransaction", 1) - if at.recoveryToken != nil { - dst = bsoncore.AppendDocumentElement(dst, "recoveryToken", at.recoveryToken) - } - return dst, nil -} - -// RecoveryToken sets the recovery token to use when committing or aborting a sharded transaction. -func (at *AbortTransaction) RecoveryToken(recoveryToken bsoncore.Document) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.recoveryToken = recoveryToken - return at -} - -// Session sets the session for this operation. -func (at *AbortTransaction) Session(session *session.Client) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.session = session - return at -} - -// ClusterClock sets the cluster clock for this operation. -func (at *AbortTransaction) ClusterClock(clock *session.ClusterClock) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.clock = clock - return at -} - -// Collection sets the collection that this command will run against. -func (at *AbortTransaction) Collection(collection string) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.collection = collection - return at -} - -// CommandMonitor sets the monitor to use for APM events. -func (at *AbortTransaction) CommandMonitor(monitor *event.CommandMonitor) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.monitor = monitor - return at -} - -// Crypt sets the Crypt object to use for automatic encryption and decryption. -func (at *AbortTransaction) Crypt(crypt driver.Crypt) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.crypt = crypt - return at -} - -// Database sets the database to run this operation against. -func (at *AbortTransaction) Database(database string) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.database = database - return at -} - -// Deployment sets the deployment to use for this operation. -func (at *AbortTransaction) Deployment(deployment driver.Deployment) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.deployment = deployment - return at -} - -// ServerSelector sets the selector used to retrieve a server. -func (at *AbortTransaction) ServerSelector(selector description.ServerSelector) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.selector = selector - return at -} - -// WriteConcern sets the write concern for this operation. -func (at *AbortTransaction) WriteConcern(writeConcern *writeconcern.WriteConcern) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.writeConcern = writeConcern - return at -} - -// Retry enables retryable mode for this operation. Retries are handled automatically in driver.Operation.Execute based -// on how the operation is set. -func (at *AbortTransaction) Retry(retry driver.RetryMode) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.retry = &retry - return at -} - -// MaxAdaptiveRetries specifies the maximum number of times the driver should retry operations -// that fail with a server side overload error. -func (at *AbortTransaction) MaxAdaptiveRetries(maxAdaptiveRetries uint) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.maxAdaptiveRetries = maxAdaptiveRetries - return at -} - -// EnableOverloadRetargeting specifies whether the driver adds the previously failed server's address -// to the list of deprioritized server addresses -func (at *AbortTransaction) EnableOverloadRetargeting(enabled bool) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.enableOverloadRetargeting = enabled - return at -} - -// ServerAPI sets the server API version for this operation. -func (at *AbortTransaction) ServerAPI(serverAPI *driver.ServerAPIOptions) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.serverAPI = serverAPI - return at -} - -// Authenticator sets the authenticator to use for this operation. -func (at *AbortTransaction) Authenticator(authenticator driver.Authenticator) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.authenticator = authenticator - return at -} - -// Logger sets the logger for this operation. -func (at *AbortTransaction) Logger(logger *logger.Logger) *AbortTransaction { - if at == nil { - at = new(AbortTransaction) - } - - at.logger = logger - return at -} diff --git a/x/mongo/driver/operation/commit_transaction.go b/x/mongo/driver/operation/commit_transaction.go deleted file mode 100644 index d9a9745d5..000000000 --- a/x/mongo/driver/operation/commit_transaction.go +++ /dev/null @@ -1,238 +0,0 @@ -// Copyright (C) MongoDB, Inc. 2019-present. -// -// Licensed under the Apache License, Version 2.0 (the "License"); you may -// not use this file except in compliance with the License. You may obtain -// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 - -package operation - -import ( - "context" - "errors" - - "go.mongodb.org/mongo-driver/v2/event" - "go.mongodb.org/mongo-driver/v2/internal/driverutil" - "go.mongodb.org/mongo-driver/v2/internal/logger" - "go.mongodb.org/mongo-driver/v2/mongo/writeconcern" - "go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore" - "go.mongodb.org/mongo-driver/v2/x/mongo/driver" - "go.mongodb.org/mongo-driver/v2/x/mongo/driver/description" - "go.mongodb.org/mongo-driver/v2/x/mongo/driver/session" -) - -// CommitTransaction attempts to commit a transaction. -type CommitTransaction struct { - authenticator driver.Authenticator - recoveryToken bsoncore.Document - session *session.Client - clock *session.ClusterClock - monitor *event.CommandMonitor - crypt driver.Crypt - database string - deployment driver.Deployment - selector description.ServerSelector - writeConcern *writeconcern.WriteConcern - retry *driver.RetryMode - maxAdaptiveRetries uint - enableOverloadRetargeting bool - serverAPI *driver.ServerAPIOptions - logger *logger.Logger -} - -// NewCommitTransaction constructs and returns a new CommitTransaction. -func NewCommitTransaction() *CommitTransaction { - return &CommitTransaction{} -} - -func (ct *CommitTransaction) processResponse(context.Context, bsoncore.Document, driver.ResponseInfo) error { - return nil -} - -// Execute runs this operations and returns an error if the operation did not execute successfully. -func (ct *CommitTransaction) Execute(ctx context.Context) error { - if ct.deployment == nil { - return errors.New("the CommitTransaction operation must have a Deployment set before Execute can be called") - } - - return driver.Operation{ - CommandFn: ct.command, - ProcessResponseFn: ct.processResponse, - RetryMode: ct.retry, - Type: driver.Write, - Client: ct.session, - Clock: ct.clock, - CommandMonitor: ct.monitor, - MaxAdaptiveRetries: ct.maxAdaptiveRetries, - EnableOverloadRetargeting: ct.enableOverloadRetargeting, - Crypt: ct.crypt, - Database: ct.database, - Deployment: ct.deployment, - Selector: ct.selector, - WriteConcern: ct.writeConcern, - ServerAPI: ct.serverAPI, - Name: driverutil.CommitTransactionOp, - Authenticator: ct.authenticator, - Logger: ct.logger, - }.Execute(ctx) -} - -func (ct *CommitTransaction) command(dst []byte, _ description.SelectedServer) ([]byte, error) { - dst = bsoncore.AppendInt32Element(dst, "commitTransaction", 1) - if ct.recoveryToken != nil { - dst = bsoncore.AppendDocumentElement(dst, "recoveryToken", ct.recoveryToken) - } - return dst, nil -} - -// RecoveryToken sets the recovery token to use when committing or aborting a sharded transaction. -func (ct *CommitTransaction) RecoveryToken(recoveryToken bsoncore.Document) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.recoveryToken = recoveryToken - return ct -} - -// Session sets the session for this operation. -func (ct *CommitTransaction) Session(session *session.Client) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.session = session - return ct -} - -// ClusterClock sets the cluster clock for this operation. -func (ct *CommitTransaction) ClusterClock(clock *session.ClusterClock) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.clock = clock - return ct -} - -// CommandMonitor sets the monitor to use for APM events. -func (ct *CommitTransaction) CommandMonitor(monitor *event.CommandMonitor) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.monitor = monitor - return ct -} - -// Crypt sets the Crypt object to use for automatic encryption and decryption. -func (ct *CommitTransaction) Crypt(crypt driver.Crypt) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.crypt = crypt - return ct -} - -// Database sets the database to run this operation against. -func (ct *CommitTransaction) Database(database string) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.database = database - return ct -} - -// Deployment sets the deployment to use for this operation. -func (ct *CommitTransaction) Deployment(deployment driver.Deployment) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.deployment = deployment - return ct -} - -// ServerSelector sets the selector used to retrieve a server. -func (ct *CommitTransaction) ServerSelector(selector description.ServerSelector) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.selector = selector - return ct -} - -// WriteConcern sets the write concern for this operation. -func (ct *CommitTransaction) WriteConcern(writeConcern *writeconcern.WriteConcern) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.writeConcern = writeConcern - return ct -} - -// Retry enables retryable mode for this operation. Retries are handled automatically in driver.Operation.Execute based -// on how the operation is set. -func (ct *CommitTransaction) Retry(retry driver.RetryMode) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.retry = &retry - return ct -} - -// MaxAdaptiveRetries specifies the maximum number of times the driver should retry operations -// that fail with a server side overload error. -func (ct *CommitTransaction) MaxAdaptiveRetries(maxAdaptiveRetries uint) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.maxAdaptiveRetries = maxAdaptiveRetries - return ct -} - -// EnableOverloadRetargeting specifies whether the driver adds the previously failed server's address -// to the list of deprioritized server addresses -func (ct *CommitTransaction) EnableOverloadRetargeting(enabled bool) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.enableOverloadRetargeting = enabled - return ct -} - -// ServerAPI sets the server API version for this operation. -func (ct *CommitTransaction) ServerAPI(serverAPI *driver.ServerAPIOptions) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.serverAPI = serverAPI - return ct -} - -// Authenticator sets the authenticator to use for this operation. -func (ct *CommitTransaction) Authenticator(authenticator driver.Authenticator) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.authenticator = authenticator - return ct -} - -// Logger sets the logger for this operation. -func (ct *CommitTransaction) Logger(logger *logger.Logger) *CommitTransaction { - if ct == nil { - ct = new(CommitTransaction) - } - - ct.logger = logger - return ct -} From e93c3576278259bfd216261310a13a43df6119ad Mon Sep 17 00:00:00 2001 From: Matt Dale <9760375+matthewdale@users.noreply.github.com> Date: Fri, 10 Jul 2026 12:40:38 -0700 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- mongo/op_abort_transaction.go | 2 -- mongo/op_commit_transaction.go | 2 -- 2 files changed, 4 deletions(-) diff --git a/mongo/op_abort_transaction.go b/mongo/op_abort_transaction.go index 6c0a2cccb..d4e5e8f61 100644 --- a/mongo/op_abort_transaction.go +++ b/mongo/op_abort_transaction.go @@ -27,7 +27,6 @@ type abortTransactionOp struct { session *session.Client clock *session.ClusterClock monitor *event.CommandMonitor - crypt driver.Crypt database string deployment driver.Deployment selector description.ServerSelector @@ -59,7 +58,6 @@ func (at *abortTransactionOp) Execute(ctx context.Context) error { CommandMonitor: at.monitor, MaxAdaptiveRetries: at.maxAdaptiveRetries, EnableOverloadRetargeting: at.enableOverloadRetargeting, - Crypt: at.crypt, Database: at.database, Deployment: at.deployment, Selector: at.selector, diff --git a/mongo/op_commit_transaction.go b/mongo/op_commit_transaction.go index 92326407c..9f7e3f858 100644 --- a/mongo/op_commit_transaction.go +++ b/mongo/op_commit_transaction.go @@ -27,7 +27,6 @@ type commitTransactionOp struct { session *session.Client clock *session.ClusterClock monitor *event.CommandMonitor - crypt driver.Crypt database string deployment driver.Deployment selector description.ServerSelector @@ -59,7 +58,6 @@ func (ct *commitTransactionOp) Execute(ctx context.Context) error { CommandMonitor: ct.monitor, MaxAdaptiveRetries: ct.maxAdaptiveRetries, EnableOverloadRetargeting: ct.enableOverloadRetargeting, - Crypt: ct.crypt, Database: ct.database, Deployment: ct.deployment, Selector: ct.selector, From 5d392f260f8ea3868acc8b7eaac497516b8ef5aa Mon Sep 17 00:00:00 2001 From: Matt Dale <9760375+matthewdale@users.noreply.github.com> Date: Tue, 14 Jul 2026 16:10:08 -0700 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Lin Borland --- mongo/op_abort_transaction.go | 2 +- mongo/op_commit_transaction.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mongo/op_abort_transaction.go b/mongo/op_abort_transaction.go index d4e5e8f61..bce4842b1 100644 --- a/mongo/op_abort_transaction.go +++ b/mongo/op_abort_transaction.go @@ -42,7 +42,7 @@ func (at *abortTransactionOp) processResponse(context.Context, bsoncore.Document return nil } -// Execute runs this operations and returns an error if the operation did not execute successfully. +// Execute runs this operation and returns an error if the operation did not execute successfully. func (at *abortTransactionOp) Execute(ctx context.Context) error { if at.deployment == nil { return errors.New("the abortTransaction operation must have a Deployment set before Execute can be called") diff --git a/mongo/op_commit_transaction.go b/mongo/op_commit_transaction.go index 9f7e3f858..bc48329de 100644 --- a/mongo/op_commit_transaction.go +++ b/mongo/op_commit_transaction.go @@ -42,7 +42,7 @@ func (ct *commitTransactionOp) processResponse(context.Context, bsoncore.Documen return nil } -// Execute runs this operations and returns an error if the operation did not execute successfully. +// Execute runs this operation and returns an error if the operation did not execute successfully. func (ct *commitTransactionOp) Execute(ctx context.Context) error { if ct.deployment == nil { return errors.New("the commitTransaction operation must have a Deployment set before Execute can be called") From 353dd8b8fe8c2c5b7dc682757daa86fcc9b883ef Mon Sep 17 00:00:00 2001 From: Matt Dale <9760375+matthewdale@users.noreply.github.com> Date: Fri, 17 Jul 2026 00:16:07 -0700 Subject: [PATCH 4/4] Unexport execute methods. --- mongo/op_abort_transaction.go | 4 ++-- mongo/op_commit_transaction.go | 4 ++-- mongo/session.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mongo/op_abort_transaction.go b/mongo/op_abort_transaction.go index bce4842b1..069661427 100644 --- a/mongo/op_abort_transaction.go +++ b/mongo/op_abort_transaction.go @@ -42,8 +42,8 @@ func (at *abortTransactionOp) processResponse(context.Context, bsoncore.Document return nil } -// Execute runs this operation and returns an error if the operation did not execute successfully. -func (at *abortTransactionOp) Execute(ctx context.Context) error { +// execute runs this operation and returns an error if the operation did not execute successfully. +func (at *abortTransactionOp) execute(ctx context.Context) error { if at.deployment == nil { return errors.New("the abortTransaction operation must have a Deployment set before Execute can be called") } diff --git a/mongo/op_commit_transaction.go b/mongo/op_commit_transaction.go index bc48329de..2008928c1 100644 --- a/mongo/op_commit_transaction.go +++ b/mongo/op_commit_transaction.go @@ -42,8 +42,8 @@ func (ct *commitTransactionOp) processResponse(context.Context, bsoncore.Documen return nil } -// Execute runs this operation and returns an error if the operation did not execute successfully. -func (ct *commitTransactionOp) Execute(ctx context.Context) error { +// execute runs this operation and returns an error if the operation did not execute successfully. +func (ct *commitTransactionOp) execute(ctx context.Context) error { if ct.deployment == nil { return errors.New("the commitTransaction operation must have a Deployment set before Execute can be called") } diff --git a/mongo/session.go b/mongo/session.go index e98a42460..3ca90f8de 100644 --- a/mongo/session.go +++ b/mongo/session.go @@ -289,7 +289,7 @@ func (s *Session) AbortTransaction(ctx context.Context) error { authenticator: s.client.authenticator, logger: s.client.logger, } - _ = op.Execute(ctx) + _ = op.execute(ctx) s.clientSession.Aborting = false _ = s.clientSession.AbortTransaction() @@ -337,7 +337,7 @@ func (s *Session) CommitTransaction(ctx context.Context) error { logger: s.client.logger, } - err = op.Execute(ctx) + err = op.execute(ctx) // Return error without updating transaction state if it is a timeout, as the transaction has not // actually been committed. if IsTimeout(err) {