Skip to content

Commit eea00c7

Browse files
committed
CSHARP-2221: Throw exception if StartTransaction is called with unacknowledged write concern.
1 parent 9c602c5 commit eea00c7

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/MongoDB.Driver.Core/Core/Bindings/CoreSession.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,12 @@ public void StartTransaction(TransactionOptions transactionOptions = null)
347347

348348
var transactionNumber = AdvanceTransactionNumber();
349349
var effectiveTransactionOptions = GetEffectiveTransactionOptions(transactionOptions);
350-
var transaction = new CoreTransaction(transactionNumber, effectiveTransactionOptions);
350+
if (!effectiveTransactionOptions.WriteConcern.IsAcknowledged)
351+
{
352+
throw new InvalidOperationException("Transactions do not support unacknowledged write concerns.");
353+
}
351354

352-
_currentTransaction = transaction;
355+
_currentTransaction = new CoreTransaction(transactionNumber, effectiveTransactionOptions);
353356
}
354357

355358
/// <inheritdoc />

tests/MongoDB.Driver.Core.Tests/Core/Bindings/CoreSessionTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.Reflection;
1718
using FluentAssertions;
1819
using MongoDB.Bson;
@@ -217,6 +218,19 @@ public void Dispose_should_have_expected_result(
217218
Mock.Get(subject.ServerSession).Verify(m => m.Dispose(), Times.Once);
218219
}
219220

221+
[Fact]
222+
public void StartTransaction_should_throw_when_write_concern_is_unacknowledged()
223+
{
224+
var cluster = CoreTestConfiguration.Cluster;
225+
var session = cluster.StartSession();
226+
var transactionOptions = new TransactionOptions(writeConcern: WriteConcern.Unacknowledged);
227+
228+
var exception = Record.Exception(() => session.StartTransaction(transactionOptions));
229+
230+
var e = exception.Should().BeOfType<InvalidOperationException>().Subject;
231+
e.Message.ToLower().Should().Contain("transactions do not support unacknowledged write concerns");
232+
}
233+
220234
[Fact]
221235
public void WasUsed_should_call_serverSession()
222236
{

0 commit comments

Comments
 (0)