Details
-
Bug
-
Resolution: Unresolved
-
Major
-
None
-
DOC-2023-S17, DOC-2023-S18, DOC-2023-S19, DOC-2023-S20, DOC-2023-S21, DOC-2023-S22, DOC-2023-S23, DOC-2023-S24, DOC-2023-S25, DOC-2023-S26
-
2
Description
The code provided in the transaction docs here https://docs.couchbase.com/dotnet-sdk/current/howtos/distributed-acid-transactions-from-the-sdk.html
doesn't work out of the box. Some of the issues are:
- Connecting to the wrong bucket and collection
- Using a .put() method on a dynamic type object.
Here is a sample piece of code fixing these issues. Happy to help redact it into more readable pieces:
using Couchbase;
|
using Couchbase.Diagnostics;
|
using Couchbase.KeyValue;
|
using Couchbase.Transactions;
|
using Couchbase.Transactions.Config;
|
using Couchbase.Transactions.Error;
|
|
class Start |
{
|
public static ICluster _cluster; |
public static IBucket _bucket; |
public static IScope _scope; |
public static ICouchbaseCollection _collection; |
public static Transactions _transactions; |
|
public static async Task Main(string[] args) |
{
|
var clusterOptions = new ClusterOptions |
{
|
ConnectionString = "couchbase://127.0.0.1", |
UserName = "Administrator", |
Password = "password", |
HttpIgnoreRemoteCertificateMismatch = true, |
KvIgnoreRemoteCertificateNameMismatch = true |
};
|
|
_cluster = await Cluster.ConnectAsync(clusterOptions).ConfigureAwait(false); |
|
await _cluster.WaitUntilReadyAsync(TimeSpan.FromSeconds(10), new WaitUntilReadyOptions()).ConfigureAwait(false); |
|
_bucket = await _cluster.BucketAsync("travel-sample").ConfigureAwait(false); |
_scope = await _bucket.ScopeAsync("inventory").ConfigureAwait(false); |
_collection = await _scope.CollectionAsync("airline").ConfigureAwait(false); |
|
_transactions = Transactions.Create(_cluster, TransactionConfigBuilder.Create());
|
|
await TransactionDemo().ConfigureAwait(false); |
}
|
|
public static async Task TransactionDemo() |
{
|
try |
{
|
var result = await _transactions.RunAsync(async (ctx) =>
|
{
|
// Inserting a doc: |
var insertedDoc = await ctx.InsertAsync(_collection, "doc-A", new { name = "New Airline" }).ConfigureAwait(false); |
|
// Getting documents: |
// Use ctx.GetAsync if the document should exist, and the transaction |
// will fail if it does not |
var docA = await ctx.GetAsync(_collection, "doc-A").ConfigureAwait(false); |
|
// Replacing a doc: |
var docB = await ctx.GetAsync(_collection, "airline_10").ConfigureAwait(false); |
var newContent = new { newContent = "newContent", other = "other" }; |
|
var replacedDoc = await ctx.ReplaceAsync(docB, newContent).ConfigureAwait(false); |
|
// Removing a doc: |
var docC = await ctx.GetAsync(_collection, "airline_10123").ConfigureAwait(false); |
await ctx.RemoveAsync(docC).ConfigureAwait(false); |
|
// This call is optional - if you leave it off, the transaction |
// will be committed anyway. |
await ctx.CommitAsync().ConfigureAwait(false); |
}).ConfigureAwait(false); |
}
|
catch (TransactionCommitAmbiguousException e) |
{
|
Console.WriteLine("Transaction possibly committed"); |
Console.WriteLine(e);
|
}
|
catch (TransactionFailedException e) |
{
|
Console.WriteLine("Transaction did not reach commit point"); |
Console.WriteLine(e);
|
}
|
}
|
}
|