Skip to content

Commit 58a73d5

Browse files
committed
CSHARP-2277: Provide Transactions example for Docs.
1 parent eedc633 commit 58a73d5

File tree

5 files changed

+310
-0
lines changed

5 files changed

+310
-0
lines changed

tests/MongoDB.Driver.Examples/MongoDB.Driver.Examples.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
<Compile Include="Properties\AssemblyInfo.cs" />
7474
<Compile Include="QueryPrimer.cs" />
7575
<Compile Include="RemovePrimer.cs" />
76+
<Compile Include="TransactionExamplesForDocs\IntroExample1.cs" />
77+
<Compile Include="TransactionExamplesForDocs\RetryExample1.cs" />
78+
<Compile Include="TransactionExamplesForDocs\RetryExample2.cs" />
79+
<Compile Include="TransactionExamplesForDocs\RetryExample3.cs" />
7680
<Compile Include="UpdatePrimer.cs" />
7781
</ItemGroup>
7882
<ItemGroup>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* Copyright 2018-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using MongoDB.Bson;
18+
19+
namespace MongoDB.Driver.Examples.TransactionExamplesForDocs
20+
{
21+
public class IntroExample1
22+
{
23+
// Start Transaction Intro Example 1
24+
public void UpdateEmployeeInfo(IMongoClient client, IClientSessionHandle session)
25+
{
26+
var employeesCollection = client.GetDatabase("hr").GetCollection<BsonDocument>("employees");
27+
var eventsCollection = client.GetDatabase("reporting").GetCollection<BsonDocument>("events");
28+
29+
session.StartTransaction(new TransactionOptions(
30+
readConcern: ReadConcern.Snapshot,
31+
writeConcern: WriteConcern.WMajority));
32+
33+
try
34+
{
35+
employeesCollection.UpdateOne(
36+
Builders<BsonDocument>.Filter.Eq("employee", 3),
37+
Builders<BsonDocument>.Update.Set("status", "Inactive"));
38+
eventsCollection.InsertOne(
39+
new BsonDocument
40+
{
41+
{ "employee", 3 },
42+
{ "status", new BsonDocument { { "new", "Inactive" }, { "old", "Active" } } }
43+
});
44+
}
45+
catch (Exception exception)
46+
{
47+
Console.WriteLine($"Caught exception during transaction, aborting: {exception.Message}.");
48+
session.AbortTransaction();
49+
throw;
50+
}
51+
52+
while (true)
53+
{
54+
try
55+
{
56+
session.CommitTransaction(); // uses write concern set at transaction start
57+
Console.WriteLine("Transaction commited.");
58+
break;
59+
}
60+
catch (MongoException exception)
61+
{
62+
// can retry commit
63+
if (exception.HasErrorLabel("UnknownTransactionCommitResult"))
64+
{
65+
Console.WriteLine("UnknownTransactionCommitResult, retrying commit operation.");
66+
continue;
67+
}
68+
else
69+
{
70+
Console.WriteLine("Error during commit.");
71+
throw;
72+
}
73+
}
74+
}
75+
}
76+
// End Transaction Intro Example 1
77+
}
78+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright 2018-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
18+
namespace MongoDB.Driver.Examples.TransactionExamplesForDocs
19+
{
20+
public class RetryExample1
21+
{
22+
// Start Transactions Retry Example 1
23+
public void RunTransactionWithRetry(Action<IMongoClient, IClientSessionHandle> txnFunc, IMongoClient client, IClientSessionHandle session)
24+
{
25+
while (true)
26+
{
27+
try
28+
{
29+
txnFunc(client, session); // performs transaction
30+
break;
31+
}
32+
catch (MongoException exception)
33+
{
34+
Console.WriteLine($"Transaction aborted. Caught exception during transaction: ${exception.Message}.");
35+
36+
// if transient error, retry the whole transaction
37+
if (exception.HasErrorLabel("TransientTransactionError"))
38+
{
39+
Console.WriteLine("TransientTransactionError, retrying transaction.");
40+
continue;
41+
}
42+
else
43+
{
44+
throw;
45+
}
46+
}
47+
}
48+
}
49+
// End Transactions Retry Example 1
50+
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright 2018-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
18+
namespace MongoDB.Driver.Examples.TransactionExamplesForDocs
19+
{
20+
public class RetryExample2
21+
{
22+
// Start Transactions Retry Example 2
23+
public void CommitWithRetry(IClientSessionHandle session)
24+
{
25+
while (true)
26+
{
27+
try
28+
{
29+
session.CommitTransaction(); // uses write concern set at transaction start
30+
Console.WriteLine("Transaction committed.");
31+
break;
32+
}
33+
catch (MongoException exception)
34+
{
35+
// can retry commit
36+
if (exception.HasErrorLabel("UnknwonTransactionCommitResult"))
37+
{
38+
Console.WriteLine("UnknownTransactionCommitResult, retrying commit operation.");
39+
continue;
40+
}
41+
else
42+
{
43+
Console.WriteLine($"Excpetion during commit: {exception.Message}.");
44+
throw;
45+
}
46+
}
47+
}
48+
}
49+
// End Transactions Retry Example 2
50+
}
51+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/* Copyright 2018-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using MongoDB.Bson;
18+
19+
namespace MongoDB.Driver.Examples.TransactionExamplesForDocs
20+
{
21+
public class RetryExample3
22+
{
23+
// Start Transactions Retry Example 3
24+
public void RunTransactionWithRetry(Action<IMongoClient, IClientSessionHandle> txnFunc, IMongoClient client, IClientSessionHandle session)
25+
{
26+
while (true)
27+
{
28+
try
29+
{
30+
txnFunc(client, session); // performs transaction
31+
break;
32+
}
33+
catch (MongoException exception)
34+
{
35+
// if transient error, retry the whole transaction
36+
if (exception.HasErrorLabel("TransientTransactionError"))
37+
{
38+
Console.WriteLine("TransientTransactionError, retrying transaction.");
39+
continue;
40+
}
41+
else
42+
{
43+
throw;
44+
}
45+
}
46+
}
47+
}
48+
49+
public void CommitWithRetry(IClientSessionHandle session)
50+
{
51+
while (true)
52+
{
53+
try
54+
{
55+
session.CommitTransaction();
56+
Console.WriteLine("Transaction committed.");
57+
break;
58+
}
59+
catch (MongoException exception)
60+
{
61+
// can retry commit
62+
if (exception.HasErrorLabel("UnknownTransactionCommitResult"))
63+
{
64+
Console.WriteLine("UnknwonTransactionCommiResult, retrying commit operation");
65+
continue;
66+
}
67+
else
68+
{
69+
Console.WriteLine($"Error during commit: {exception.Message}.");
70+
throw;
71+
}
72+
}
73+
}
74+
}
75+
76+
// updates two collections in a transaction
77+
public void UpdateEmployeeInfo(IMongoClient client, IClientSessionHandle session)
78+
{
79+
var employeesCollection = client.GetDatabase("hr").GetCollection<BsonDocument>("employees");
80+
var eventsCollection = client.GetDatabase("reporting").GetCollection<BsonDocument>("events");
81+
82+
session.StartTransaction(new TransactionOptions(
83+
readConcern: ReadConcern.Snapshot,
84+
writeConcern: WriteConcern.WMajority));
85+
86+
try
87+
{
88+
employeesCollection.UpdateOne(
89+
Builders<BsonDocument>.Filter.Eq("employee", 3),
90+
Builders<BsonDocument>.Update.Set("status", "Inactive"));
91+
eventsCollection.InsertOne(
92+
new BsonDocument
93+
{
94+
{ "employee", 3 },
95+
{ "status", new BsonDocument { { "new", "Inactive" }, { "old", "Active" } } }
96+
});
97+
}
98+
catch (Exception exception)
99+
{
100+
Console.WriteLine($"Caught exception during transaction, aborting: {exception.Message}.");
101+
session.AbortTransaction();
102+
throw;
103+
}
104+
105+
CommitWithRetry(session);
106+
}
107+
108+
public void UpdateEmployeeInfoWithTransactionRetry(IMongoClient client)
109+
{
110+
// start a session
111+
using (var session = client.StartSession())
112+
{
113+
try
114+
{
115+
RunTransactionWithRetry(UpdateEmployeeInfo, client, session);
116+
}
117+
catch (Exception exception)
118+
{
119+
// do something with error
120+
Console.WriteLine($"Non transient exception caught during transaction: ${exception.Message}.");
121+
}
122+
}
123+
}
124+
// End Transactions Retry Example 3
125+
}
126+
}

0 commit comments

Comments
 (0)