Skip to content

Commit 02322bc

Browse files
tmasternakrstam
authored andcommitted
CSHARP-2822: StartTransaction throws for Standalone and Unknown servers
1 parent ddbf505 commit 02322bc

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,13 @@ private void EnsureTransactionsAreSupported()
467467

468468
foreach (var connectedDataBearingServer in connectedDataBearingServers)
469469
{
470-
if (connectedDataBearingServer.Type == ServerType.ShardRouter)
470+
var serverType = connectedDataBearingServer.Type;
471+
472+
if(serverType == ServerType.Standalone || serverType == ServerType.Unknown)
473+
{
474+
throw new NotSupportedException("Transactions are supported only in sharded cluster of replica set deployments.");
475+
}
476+
else if (serverType == ServerType.ShardRouter)
471477
{
472478
Feature.ShardedTransactions.ThrowIfNotSupported(connectedDataBearingServer.Version);
473479
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ public void EnsureTransactionsAreSupported_should_throw_when_there_are_no_connec
325325
}
326326

327327
[Theory]
328+
[InlineData("NT")]
329+
[InlineData("UT")]
328330
[InlineData("PN")]
329331
[InlineData("PN,ST")]
330332
[InlineData("PT,SN")]
@@ -358,7 +360,11 @@ public void EnsureTransactionsAreSupported_should_throw_when_any_connected_data_
358360
var exception = Record.Exception(() => subject.EnsureTransactionsAreSupported());
359361

360362
var e = exception.Should().BeOfType<NotSupportedException>().Subject;
361-
e.Message.Should().Contain($"does not support the {unsupportedFeatureName} feature.");
363+
e.Message.Should().Match<string>(
364+
s => s.Contains($"does not support the {unsupportedFeatureName} feature.") ||
365+
s.Contains("Transactions are supported only in sharded cluster of replica set deployments.") ||
366+
s.Contains("StartTransaction cannot determine if transactions are supported because there are no connected servers.")
367+
);
362368
}
363369

364370
// private methods
@@ -446,6 +452,7 @@ private ServerType MapServerTypeCode(char code)
446452
switch (code)
447453
{
448454
case 'A': return ServerType.ReplicaSetArbiter;
455+
case 'N': return ServerType.Standalone;
449456
case 'P': return ServerType.ReplicaSetPrimary;
450457
case 'R': return ServerType.ShardRouter;
451458
case 'S': return ServerType.ReplicaSetSecondary;

0 commit comments

Comments
 (0)